尝试模拟用户时获取访问被拒绝

问题描述:

我想要完成的是模拟控制台应用程序的特定用户。我已经研究过这一点,试图找到解决方案,但我不断收到访问被拒绝的错误。这是我在下面做的。请任何帮助,将不胜感激,我已经为此工作了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

我比一年前完成了这一步,我可以说它的代码行太少(尤其是从DLL导入的函数太少)。我现在不记得所有的东西,也没有我的旧代码,但绝对是你的代码太简单了。最后,我甚至没有提供其他用户凭证,但需要从Winapi导入更多不安全的功能。 – ElmoVanKielmo

+0

是的,我的凭据是正确的。我真的不知道还有什么要尝试查看问题的原因 – Will

除非你有特殊需要使用的LogonUser ANSI版本,你应该在你的声明中使用LogonUser的,而不是LogonUserA,即

Declare Function LogonUser Lib "advapi32.dll" 

还应验证用户被冒充有本地机器上的交互式登录权限。

+0

尝试它时,我收到另一个错误,指出它没有接入点 – Will