Vb.net 尝试模拟用户时访问被拒绝

Vb.net 尝试模拟用户时访问被拒绝,vb.net,Vb.net,我试图完成的是模拟控制台应用程序的特定用户。我已经对此进行了研究,试图找到解决方案,但我一直收到拒绝访问错误。下面是我正在做的事情。请任何帮助将不胜感激,我已经为此工作了4天了 Imports System.Security Imports System.Security.Principal Imports System.Runtime.InteropServices Imports System.Security.Permissions Dim impersonationContext A

我试图完成的是模拟控制台应用程序的特定用户。我已经对此进行了研究,试图找到解决方案,但我一直收到拒绝访问错误。下面是我正在做的事情。请任何帮助将不胜感激,我已经为此工作了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
导入系统安全性
导入System.Security.Principal
导入System.Runtime.InteropServices
导入System.Security.Permissions
将impersonationContext设置为WindowsImpersonationContext
声明函数LogonUserA Lib“advapi32.dll”(ByVal lpszUsername作为字符串_
ByVal lpszDomain作为字符串_
ByVal LPSZ密码作为字符串_
ByVal dwLogonType为整数_
ByVal dwLogonProvider作为整数_
ByRef phToken作为IntPtr)作为整数
声明自动函数DuplicateToken库“advapi32.dll”(_
ByVal ExistingTokenHandle作为IntPtr_
ByVal ImpersonationLevel为整数_
ByRef DuplicateTokenHandle(作为IntPtr)作为整数
将自动函数RevertToSelf Lib“advapi32.dll”()声明为
将自动函数CloseHandle Lib“kernel32.dll”(ByVal句柄为IntPtr)声明为
Public Sub-Main(ByVal args作为字符串())
Dim w作为StreamWriter
Dim filepath为String=“C:\test\u files\testFile.txt”
将新字符串变暗为字符串
new_string=“”
尝试
如果impersonateValidUser(“用户名”、“域”、“密码”),则
'在此处插入在特定用户的安全上下文下运行的代码。
'取消模拟()
其他的
'您的模拟失败。因此,这里包括一个故障安全机制。
如果结束
new_string=“Worked”&System.Security.Principal.WindowsIdentity.GetCurrent.Name
特例
new_string=“未工作:”&ex.消息
最后
如果System.IO.File.Exists(文件路径),则
File.Delete(文件路径)
如果结束
w=File.CreateText(文件路径)
w、 WriteLine(新字符串)
w、 刷新()
w、 关闭()
'myConnection.Close()
结束尝试
端接头
私有函数impersonateValidUser(ByVal用户名作为字符串,ByVal域作为字符串,ByVal密码作为字符串)作为布尔值
将tempWindowsIdentity设置为WindowsIdentity
作为IntPtr=IntPtr.Zero的Dim令牌
Dim标记复制为IntPtr=IntPtr.Zero
impersonateValidUser=False
如果返回到self(),则
如果LogonUserA(用户名、域、密码、LOGON32\u LOGON\u INTERACTIVE、,
LOGON32\u提供程序\u默认值,令牌)0然后
如果DuplicateToken(token,2,tokenDuplicate)0,则
tempWindowsIdentity=新WindowsIdentity(令牌重复)
impersonationContext=tempWindowsIdentity.Impersonate()
如果不是,则impersonationContext什么都不是
impersonateValidUser=True
如果结束
如果结束
如果结束
如果结束
如果不是重复.Equals(IntPtr.Zero),则
CloseHandle(重复标记)
如果结束
如果不是token.Equals(IntPtr.Zero),则
CloseHandle(令牌)
如果结束
端函数

除非您特别需要使用的ANSI版本,否则您应该在声明中使用LogonUser而不是LogonUserA,即

Declare Function LogonUser Lib "advapi32.dll"

您还应验证被模拟的用户在本地计算机上是否具有交互式登录权限。

您确定输入的凭据是ringt吗?您确定您输入的凭据具有管理权限吗?我在一年多前就这样做了,可以说代码行太少(尤其是从DLL导入的函数太少)。我现在什么都不记得了,这里也没有我的旧代码,但你的代码肯定太简单了。最后,我甚至不需要提供其他用户凭据,但需要从Winapi导入更多不安全的函数。是的,我的凭据是正确的。老实说,我不知道还有什么可以尝试看看问题是什么当我尝试它时,我得到另一个错误,说明它没有接入点