解决方案(有点):

事实证明,这种对 .NET 安全性的模拟只允许应用程序级访问。由于COM对象处于系统级别,所以被模拟的用户仍然无法实例化它。我通过右键单击可执行文件并选择“运行方式...”来解决这个问题,该程序运行良好。我发现使用系统访问权限启动程序(假设您运行该程序的用户具有这些凭据)。现在我正在创建一个外部程序,该程序将使用此方法启动此应用程序。

谢谢你的提示:D


我在虚拟机上安装了 Windows XP。它是我的域的一部分,但登录的用户只是本地用户。显然,如果我尝试访问网络共享,它将提示输入用户/密码:

alt text

我在虚拟机上测试的程序使用 COM 对象与来自另一个程序的数据进行交互。如果我不模仿,我会收到错误,因为我没有适当的凭据。

我对这个问题做了一些研究,发现许多网站都有大量的 VB.NET 信息。我编写的代码遇到的问题是我可以访问网络资源,但无法实例化 COM 对象。

如果我在尝试实例化它之前填写并提交凭据提示(上面),它就可以正常工作。这让我相信 WinXP 凭据提示一定做了一些我没有做的事情。下面是我用于模拟的代码:

Public Sub BeginImpersonation()
    Const LOGON32_PROVIDER_DEFAULT As Integer = 0
    Const LOGON32_LOGON_INTERACTIVE As Integer = 2
    Const SecurityImpersonation As Integer = 2

    Dim win32ErrorNumber As Integer

    _tokenHandle = IntPtr.Zero
    _dupeTokenHandle = IntPtr.Zero

    If Not LogonUser(_username, _domainname, _password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, _tokenHandle) Then
        win32ErrorNumber = System.Runtime.InteropServices.Marshal.GetLastWin32Error()
        Throw New ImpersonationException(win32ErrorNumber, GetErrorMessage(win32ErrorNumber), _username, _domainname)
    End If

    If Not DuplicateToken(_tokenHandle, SecurityImpersonation, _dupeTokenHandle) Then
        win32ErrorNumber = System.Runtime.InteropServices.Marshal.GetLastWin32Error()

        CloseHandle(_tokenHandle)
        Throw New ImpersonationException(win32ErrorNumber, "Unable to duplicate token!", _username, _domainname)
    End If

    Dim newId As New System.Security.Principal.WindowsIdentity(_dupeTokenHandle)
    _impersonatedUser = newId.Impersonate()
    _impersonating = True
End Sub

我还尝试向模仿者方法发送不同的标志,但似乎没有任何效果。以下是我发现的不同标志:

Enum LOGON32_LOGON
    INTERACTIVE = 2
    NETWORK = 3
    BATCH = 4
    SERVICE = 5
    UNLOCK = 7
    NETWORK_CLEARTEXT = 8
    NEW_CREDENTIALS = 9
End Enum
Enum LOGON32_PROVIDER
    [DEFAULT] = 0
    WINNT35 = 1
    WINNT40 = 2
    WINNT50 = 3
End Enum
Enum SECURITY_LEVEL
    Anonymous = 0
    Identification = 1
    Impersonation = 2
    Delegation = 3
End Enum
有帮助吗?

解决方案

我之前遇到过这种情况,使用了两种不同的解决方案 - 最简单的是使用第三方应用程序:TqcRunas: http://www.quimeras.com/Products/products.asp ,它允许您将所需的creentials打包在加密文件中。但是,如果密码被迫过期,则会很痛苦。

我使用的另一个解决方案是使用备用凭据调用新进程:

        Dim myProcessStartInfo As ProcessStartInfo = New ProcessStartInfo

    With myProcessStartInfo

        .FileName = "file path and name"

        .Domain = "domainname"
        .UserName = "username"

        'password needs to be a SerureString
        Using NewPassword As New Security.SecureString
            With NewPassword
                For Each c As Char In "password".ToCharArray
                    .AppendChar(c)
                Next c
                .MakeReadOnly()
            End With
            .Password = NewPassword.Copy
        End Using

        'UseShellExecute must be false for impersonated process
        .UseShellExecute = False

    End With

    Using Process As New System.Diagnostics.Process
        With Process
            .StartInfo = myProcessStartInfo
            .Start()
        End With
    End Using

其他提示

根据您的定义,我使用

LogonUser(_username, _domainname, _password, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50, _tokenHandle)

在我的代码中,通过网络进行身份验证。我像你一样冒充远程盒子上的本地用户。很久以前,我不记得使用这些值的理由。

我做了类似的事情来映射网络驱动器以在计算机之间复制文件。我没有编写代码,但它与你的代码几乎相同,除了两件事:

  • Impersonate 方法返回后,我会使用 CloseHandle 例程关闭这两个令牌,然后再退出 impersonator 方法。

  • 在模仿者的顶部,发生的第一件事是调用 RevertToSelf,大概是为了取消任何先前的模仿。

我不知道他们是否会有所作为,但值得一试。以下是相关声明:

Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long
Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top