Hello,
I am still struggling with impersonation in vb.net. I have a service that
logs on to exchange but I get different results when using the "log on as"
feature on the service as opposed to the impersonation routine. I am very
curious about how that is possible. It should yield the same results
Has anyone else used impersonation in a vb.net service?
The routine i am using is from an article on msdn (if i recall correctly)
and is printed at the end of this message. I run this at the very start of
the service, and do the Undo() function when the service ends so that ALL
the code in the service is supposed to run as the given user.
Thanks for any feedback,
Rune
Public 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