Impersonation In Windows 7
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), _userna开发者_开发问答me, _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()
精彩评论