Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

impersonate user in windows forms

16 views
Skip to first unread message

RTT

unread,
May 4, 2005, 6:44:39 AM5/4/05
to
i'm writing a windows form but codebased a iwant to run the code as a
different user.

like in a webapplication you can impersonate a user so the website does not
run on the standard ASP.NET user.

is it possible to do the same for a windows form and define a user codebased
and run the code like that user is running the application.


scorpion53061

unread,
May 4, 2005, 6:54:59 AM5/4/05
to
The following example demonstrates how to impersonate a user and then
revert
to the original identity.
[Visual Basic]
Imports System
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.Security.Permissions

<Assembly:SecurityPermissionAttribute(SecurityAction.RequestMinimum,
UnmanagedCode := true)>
Public Class Impersonation

<DllImport("C:\\WINNT\\System32\\advapi32.dll")> _
Public Shared Function LogonUser(lpszUsername As String, lpszDomain As
String, lpszPassword As String, _
dwLogonType As Integer, dwLogonProvider As Integer, ByRef
phToken As Integer) As Boolean
End Function

<DllImport("C:\\WINNT\\System32\\Kernel32.dll")> _
Public Shared Function GetLastError() As Integer
End Function

Public Shared Sub Main(args() As String)

'The Windows NT user token.
Dim token1 As Integer

'Get the user token for the specified user, machine, and password
using the unmanaged LogonUser method.

'The parameters for LogonUser are the user name, computer name,
password,
'Logon type (LOGON32_LOGON_NETWORK_CLEARTEXT), Logon provider
(LOGON32_PROVIDER_DEFAULT),
'and user token.
Dim loggedOn As Boolean = LogonUser("bob", "AARDVARK", "coffee", 3, 0,
token1)
Console.WriteLine("LogonUser called")

'Call GetLastError to try to determine why logon failed if it did not
succeed.
Dim ret As Integer = GetLastError()

Console.WriteLine("LogonUser Success? " + loggedOn)
Console.WriteLine("NT Token Value: " + token1)
If ret <> 0 Then
Console.WriteLine("Error code (126 == ""Specified module could not
be found""): " + ret)
End If

'Starting impersonation here:
Console.WriteLine("Before impersonation:")
Dim mWI1 As WindowsIdentity = WindowsIdentity.GetCurrent()
Console.WriteLine(mWI1.Name)
Console.WriteLine(mWI1.Token)

Dim token2 As IntPtr = new IntPtr(token1)

Console.WriteLine("New identity created:")
Dim mWI2 As WindowsIdentity = new WindowsIdentity(token2)
Console.WriteLine(mWI2.Name)
Console.WriteLine(mWI2.Token)

'Impersonate the user.
Dim mWIC As WindowsImpersonationContext = mWI2.Impersonate()

Console.WriteLine("After impersonation:")
Dim mWI3 As WindowsIdentity = WindowsIdentity.GetCurrent()
Console.WriteLine(mWI3.Name)
Console.WriteLine(mWI3.Token)

'Revert to previous identity.
mWIC.Undo()

Console.WriteLine("After impersonation is reverted:")
Dim mWI4 As WindowsIdentity = WindowsIdentity.GetCurrent()
Console.WriteLine(mWI4.Name)
Console.WriteLine(mWI4.Token)
End Sub
End Class

"RTT" <R...@pandora.be> wrote in message news:R...@pandora.be:

RTT

unread,
May 4, 2005, 7:45:47 AM5/4/05
to
nice...

but he give the error that he can't find the specified modules... but when i
look they are in the folder specified...

<DllImport("C:\\WINNT\system32\\ADVAPI32.DLL")>

but the file does exist. any ideas?

thxs in advance

"scorpion53061" <scorpio...@nospamhereyahoo.com> wrote in message
news:%235MFBfJ...@TK2MSFTNGP10.phx.gbl...

Cor Ligthert

unread,
May 4, 2005, 7:54:08 AM5/4/05
to
RTT,

Why do you not search for it?


Cor


RTT

unread,
May 4, 2005, 8:05:49 AM5/4/05
to
what do you mean search?

the dll's are in de folder like specified in the code. but he can't load it.

search? other way to load?

sorry, first time working with dll's so much :s


"Cor Ligthert" <notmyfi...@planet.nl> wrote in message
news:efP9wAK...@tk2msftngp13.phx.gbl...

scorpion53061

unread,
May 4, 2005, 10:56:01 AM5/4/05
to

I don't know what to tell you.

Maybe something is wrong with your computer?

Herfried K. Wagner [MVP]

unread,
May 4, 2005, 12:16:43 PM5/4/05
to
"RTT" <R...@pandora.be> schrieb:

> but he give the error that he can't find the specified modules... but when
> i
> look they are in the folder specified...
>
> <DllImport("C:\\WINNT\system32\\ADVAPI32.DLL")>
>
> but the file does exist. any ideas?

Simply specify the file name only ('<DllImport("ADVAPI32.DLL")>'). Double
backslashes are /not/ required in VB.NET!

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Cor Ligthert

unread,
May 4, 2005, 12:34:30 PM5/4/05
to

Cor Ligthert

unread,
May 4, 2005, 12:36:45 PM5/4/05
to
RTT,

Did you set a reference to the namespace in this page?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemruntimeinteropservicesdllimportattributeclasstopic.asp

(I don't know if I linked the message to the right question)

0 new messages