468,512 Members | 1,341 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

vbusing impersonation in vb.net

h
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
Nov 20 '05 #1
0 3806

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Ripa Horatiu | last post: by
11 posts views Thread by Phil | last post: by
1 post views Thread by Patrick | last post: by
reply views Thread by velvet.graham | last post: by
5 posts views Thread by =?Utf-8?B?S2l0dHlIYXdr?= | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.