How do I use this module?

waughp

Member
Joined
Feb 18, 2005
Messages
15
Programming Experience
Beginner
Hey Experts,

I have a module that I got from a book that is supposed to handle impersonation. I have no idea how to incorporate this into my code and it's the last big piece I need to get the entire program working how I want. Here is my module:

Imports System.Security.Principal

Module RunAs

'This API function gets the security token for a user.

PrivateDeclareAutoFunction LogonUser Lib "advapi32.dll" _

(
ByVal lpszUsername AsString, ByVal lpszDomain AsString, _

ByVal lpszPassword AsString, ByVal dwlogonType AsInteger, _

ByVal dwLogonProvider AsInteger, ByRef phToken As IntPtr) AsInteger

PrivateEnum Logon

Interactive = 2

NetworkCleartext = 8

EndEnum

PrivateEnum Provider

[Default] = 0

WindowsNT25 = 1

WindowsNT40 = 2

Windows2000 = 3

EndEnum

'This API function duplicates a security token so you can use it.

PrivateDeclareAutoFunction DuplicateToken Lib "advapi32.dll" _

(
ByVal ExistingTokenHandle As IntPtr, _

ByVal ImpersonationLevel AsInteger, _

ByRef DuplicateTokenHandle As IntPtr) AsInteger

PublicSub Main()

Console.WriteLine("*** Current User ****")

DisplayIdentityInfo()

' Can I hardcode the user/pass in the code so my user is not prompted?'
' Login Information


Dim MyUser, MyPass
Username = MyUser
Password = MyPass
Domain = localhost


' Log the new identity in

Dim NewIdentity As WindowsIdentity

NewIdentity = GetWindowsIdentity(UserName, Domain, Password)

Console.WriteLine()

If NewIdentity IsNothingThen

Console.WriteLine("Invalid credentials.")

Else

' Impersonate the new identity

Dim NewContext As WindowsImpersonationContext

NewContext = NewIdentity.Impersonate

Console.WriteLine("*** Starting Runas ***")

DisplayIdentityInfo()

EndIf

Console.ReadLine()

EndSub

' This function displays information about the current user.

PrivateSub DisplayIdentityInfo()

Dim Identity As WindowsIdentity = WindowsIdentity.GetCurrent()

Console.WriteLine("ATSS is now executing as " & Identity.Name)

Console.WriteLine()

EndSub

' This function uses the Win32 API functions to return a WindowsIdentity object for a given user

PrivateFunction GetWindowsIdentity(ByVal UserName AsString, _

ByVal Domain AsString, ByVal Password AsString) As WindowsIdentity

Dim SecurityToken, TokenDuplicate As IntPtr

If LogonUser(UserName, Domain, Password, _

Logon.Interactive, Provider.Default, SecurityToken) > 0
Then

DuplicateToken(SecurityToken, 2, TokenDuplicate)

ReturnNew WindowsIdentity(TokenDuplicate)

Else

' Invalid user information

ReturnNothing

EndIf

EndFunction

End
Module

Now from What I've read in other forums, the context has something to do on the lines of...undo.impersonation or something like that, but i'm not sure how to get it all working. Hopefully one of you experts will be able to read this module and point me in the right direction.

Thanks in advance!

Pat
 
If there is anybody out there that knows how to use impersonation (run portions of a program as a different user) please help me out!

Thank you!

Pat
 
Back
Top