2013-07-01 2 views
0

내가 수행하려고하는 것은 콘솔 응용 프로그램의 특정 사용자를 가장하는 것입니다. 솔루션을 찾으려고 이걸 조사했지만 계속 액세스가 거부되었습니다. 여기에 제가하고있는 일이 있습니다. 어떤 도움을 주시면 감사하겠습니다. 저는 4 일 동안이 작업을하고 있습니다.사용자를 가장하려고하면 액세스가 거부됩니다.

Imports System.Security 
Imports System.Security.Principal 

Imports System.Runtime.InteropServices 
Imports System.Security.Permissions 

Dim impersonationContext As WindowsImpersonationContext 

Declare Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As String, _ 
         ByVal lpszDomain As String, _ 
         ByVal lpszPassword As String, _ 
         ByVal dwLogonType As Integer, _ 
         ByVal dwLogonProvider As Integer, _ 
         ByRef phToken As IntPtr) As Integer 

Declare Auto Function DuplicateToken Lib "advapi32.dll" (_ 
         ByVal ExistingTokenHandle As IntPtr, _ 
         ByVal ImpersonationLevel As Integer, _ 
         ByRef DuplicateTokenHandle As IntPtr) As Integer 

Declare Auto Function RevertToSelf Lib "advapi32.dll"() As Long 
Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long 

Public Sub Main(ByVal args As String()) 

    Dim w As StreamWriter 
    Dim filepath As String = "C:\test_files\testFile.txt" 

    Dim new_string As String 
    new_string = "" 

    Try 
     If impersonateValidUser("USERNAME", "DOMAIN", "PASSWORD") Then 
      'Insert your code that runs under the security context of a specific user here. 
      'undoImpersonation() 
     Else 
      'Your impersonation failed. Therefore, include a fail-safe mechanism here. 
     End If 

     new_string = "Worked " & System.Security.Principal.WindowsIdentity.GetCurrent.Name 

    Catch ex As Exception 
     new_string = "Didnt work: " & ex.Message 
    Finally 

     If System.IO.File.Exists(filepath) Then 
      File.Delete(filepath) 
     End If 

     w = File.CreateText(filepath) 

     w.WriteLine(new_string) 
     w.Flush() 
     w.Close() 

     'myConnection.Close() 
    End Try 

End Sub 

Private Function impersonateValidUser(ByVal userName As String, ByVal domain As String, ByVal password As String) As Boolean 

    Dim tempWindowsIdentity As WindowsIdentity 
    Dim token As IntPtr = IntPtr.Zero 
    Dim tokenDuplicate As IntPtr = IntPtr.Zero 
    impersonateValidUser = False 

    If RevertToSelf() Then 
     If LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, 
        LOGON32_PROVIDER_DEFAULT, token) <> 0 Then 
      If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then 
       tempWindowsIdentity = New WindowsIdentity(tokenDuplicate) 
       impersonationContext = tempWindowsIdentity.Impersonate() 
       If Not impersonationContext Is Nothing Then 
        impersonateValidUser = True 
       End If 
      End If 
     End If 
    End If 
    If Not tokenDuplicate.Equals(IntPtr.Zero) Then 
     CloseHandle(tokenDuplicate) 
    End If 
    If Not token.Equals(IntPtr.Zero) Then 
     CloseHandle(token) 
    End If 
End Function 
+0

입력 한 자격 증명이 울리는 것이 확실합니까? 입력 한 자격 증명에 관리 권한이 있는지 확인하십시오. –

+0

나는 1 년 전에이 작업을 해왔으며, 코드가 너무 적다는 것을 말할 수 있습니다 (특히 DLL에서 함수 가져 오기가 너무 적음). 나는 지금 모든 것을 기억하지 않고 여기에 나의 오래된 코드를 가지고 있지 않지만 당신의 코드는 너무 단순하다. 결국에는 다른 사용자 자격 증명을 제공 할 필요조차 없지만 Winapi에서 안전하지 않은 기능을 더 많이 가져와야했습니다. – ElmoVanKielmo

+0

예 자격 증명이 정확합니다. 나는 솔직히 그 문제가 무엇인지 알아 내려고하지 않을 것입니다. – Will

답변

0

당신이 LogonUser의 ANSI 버전을 사용하기 위해 특별히 필요하지 않으면, 당신은 당신의 선언에 대신 LogonUserA의 LogonUser를 사용

이 가 당신은 또한 사용자가 도용되고 있는지 확인해야합니다

이있다

Declare Function LogonUser Lib "advapi32.dll" 
즉한다 로컬 컴퓨터의 대화 형 로그온 권한

+0

시도했을 때, 액세스 포인트가 없다는 또 다른 오류가 나타납니다. – Will

관련 문제