Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Impersonation

Status
Not open for further replies.

RubenZ

Technical User
Sep 10, 2002
17
DE
Hi!

I've found a piece of code which should help me to impersonate within a
program.
When I put in the piece in my Studio.net and I compile it, it fails to
authenticate. The MsgBox("Authentication Faild!") is shown then.

I'm a VB.NET beginner and I've no idea how to solve the problem.

Thanks in advanced

RubenZ


Module Module 1


Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As
String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal
dwLogonType As Integer, ByVal dwLogonProvider As Integer, ByRef phToken As
IntPtr) As Integer
Declare Auto Function ImpersonateLoggedOnUser Lib "advapi32.dll" (ByVal
hToken As IntPtr) As Integer
Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Integer

Sub Main()

If impersonateValidUser("Peter", "MyDomain", "PetersPassword") then

System.Diagnostics.Process.Start("C:\windows\notepad.exe")
undoImpersonation()
else
MsgBox("Authentication Failed!")
end if

End Sub


Function impersonateValidUser(username as String, Domain as String, password
as
String) as boolean
Dim LogonType as Integer
Dim LogonProvider as Integer
Dim Tk as IntPtr

LogonType = 2 'Interactive
LogonProvider = 0 'Default Provider

If LogonUser(username ,Domain ,password , LogonType, LogonProvider ,Tk) <> 0
then
if ImpersonateLoggedonUser(Tk) <> 0 then
impersonateValidUser ="True"
else
impersonateValidUser ="False"
End if
End If

End function

Sub undoImpersonation()
RevertToSelf()
End Sub

End Module
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top