用.NET的安全性certificate了这种模仿只允许应用程序级的访问。 由于COM对象在系统级别,所以模拟的用户仍然不能实例化它。 我想通过右键单击可执行文件并select“运行为…”,程序运行正常。 我发现启动程序与系统访问(假设你正在运行的用户有这些凭据)。 现在我正在创build一个外部程序,用这个方法启动这个应用程序。
感谢提示:D
我有一个虚拟机上的Windows XP安装。 它是我的域的一部分,但login的用户只是本地用户。 显然,如果我尝试访问networking共享,它会提示input用户名和密码:
替代文字http://img.zgserver.com/windows/wchl5l.jpg
我在虚拟机上testing的程序使用COM对象与另一个程序的数据进行交互。 如果我不冒充,我得到错误,因为我没有适当的凭据。
我对这个问题做了一些研究,发现了一些有相当数量的VB.NET信息的网站。 我用我写的代码的问题是我可以访问networking资源,但我不能实例化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
我之前遇到过这种情况,并使用了两种不同的soloution – 最简单的方法是使用第三方应用程序:TQcRunas: http ://www.quimeras.com/Products/products.asp,它允许您将所需的屏幕保护程序打包到加密文件。 然而,如果密码被迫过期则是一种痛苦。
我使用的另一个解决方案是使用其他凭据调用新的流程:
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
请参阅ImpersonationHelper 是否可以安全地从VB.NET获取SecureString值?