problems with Impersonate

From: Juan (Juan_at_discussions.microsoft.com)
Date: 08/16/04


Date: Mon, 16 Aug 2004 08:39:04 -0700

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



Relevant Pages

  • Re: Removal of a service
    ... Shared Sub MainAs String) ... Private Const SERVICE_START As Integer = &H10 ... Declare Auto Function CloseServiceHandle Lib "advapi32.dll" (ByVal ...
    (microsoft.public.dotnet.languages.vb)
  • logonuser api returns 127
    ... Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As ... String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal ... Dim LogonProvider As Integer ... impersonateValidUser = False ...
    (microsoft.public.dotnet.languages.vb)
  • Re: problems with Impersonate
    ... String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal ... Declare Auto Function RevertToSelf Lib "advapi32.dll" As Integer ... Sub Main ...
    (microsoft.public.dotnet.languages.vb)
  • Re: check a running process, impersonate
    ... Dim LOGON32_LOGON_INTERACTIVE As Integer = 2 ... Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As ... Public Sub Page_Load ... Private Function impersonateValidUser(userName As String, ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: logonuser api returns 127
    ... > String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal ... > Dim LogonProvider As Integer ... > impersonateValidUser = False ... are you absolutely sure that GetLastError is returning ...
    (microsoft.public.dotnet.languages.vb)

Loading