problems with Impersonate

G

Guest

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

Juan


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
 
K

Ken Tucker [MVP]

Hi,

http://www.dotnet247.com/247reference/msgs/28/144136.aspx

Ken
------------------

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

Juan


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top