Windows 7 中的模拟
我有一个模拟类,到目前为止在 Windows XP 上运行得很好,但是当我尝试在 Windows 7 上使用相同的模拟时,我遇到了问题。据我所知,模拟是有效的,但是当我尝试访问该用户下的文件时,我收到访问被拒绝的错误。还有其他人遇到过这个问题并找到解决方法吗?
公共子 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()
结束子
Dim aa As fcGlobals.AliasAccount
'Use Impersonation
aa = New fcGlobals.AliasAccount("xxxx", "xxxx")
aa.BeginImpersonation()
For a = 0 To mPages.Count - 1
If System.IO.File.Exists(mFileNames(a)) = True Then
System.IO.File.Delete(mFileNames(a))
End If
Next
aa.EndImpersonation()
aa.Dispose()
I have an impersonation class that so far has been working great on windows XP, however when I try to use the same Impersonation on Windows 7 I have an issue. The impersonation as far as I know works but when I try and access files under this user I get the access denied error. Has anyone else run into this issue and found a fix?
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()
End Sub
Dim aa As fcGlobals.AliasAccount
'Use Impersonation
aa = New fcGlobals.AliasAccount("xxxx", "xxxx")
aa.BeginImpersonation()
For a = 0 To mPages.Count - 1
If System.IO.File.Exists(mFileNames(a)) = True Then
System.IO.File.Delete(mFileNames(a))
End If
Next
aa.EndImpersonation()
aa.Dispose()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论