472,371 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

impersonate user in windows forms

RTT
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.
Nov 21 '05 #1
8 16904
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(SecurityActi on.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" <RT*@pandora.be> wrote in message news:RT*@pandora.be:
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.


Nov 21 '05 #2
RTT
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" <sc************@nospamhereyahoo.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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(SecurityActi on.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" <RT*@pandora.be> wrote in message news:RT*@pandora.be:
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.

Nov 21 '05 #3
RTT,

Why do you not search for it?
Cor
Nov 21 '05 #4
RTT
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" <no************@planet.nl> wrote in message
news:ef*************@tk2msftngp13.phx.gbl...
RTT,

Why do you not search for it?
Cor

Nov 21 '05 #5

I don't know what to tell you.

Maybe something is wrong with your computer?

"RTT" <RT*@pandora.be> wrote in message news:RT*@pandora.be:
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" <sc************@nospamhereyahoo.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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(SecurityActi on.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" <RT*@pandora.be> wrote in message news:RT*@pandora.be:
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.


Nov 21 '05 #6
"RTT" <RT*@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/>

Nov 21 '05 #7
RTT,

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

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor
Nov 21 '05 #8
RTT,

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

http://msdn.microsoft.com/library/de...classtopic.asp

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

I hope this helps,

Cor

Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Greg Bacchus | last post by:
Hi, I'm getting an exception that really has me stumped. It's sporadic at best, it's only happened a handful of times. This particular time it happened when the user pressed 'Alt-S' to save the...
0
by: Tim Haughton | last post by:
I've just released an article on using Test Driven Development with C# and Windows Forms. GUI's are often difficult to test, so I thought it might be of interest. The article along with the...
15
by: Wiktor Zychla | last post by:
today we've found a critical issue regarding the ListView from Windows.Forms. it was confirmed on several machines with Win2K and XP. here's the problem: create a ListView with about 50000 rows....
3
by: Chris Dunaway | last post by:
A quick scan of the group did not immediately reveal an answer to my questions so here goes. First let me describe my app and then I'll ask the questions. I am writing a Windows Forms App (not...
6
by: richi | last post by:
Hi... Okay this is driving me mad. I have a very simple webpage served up from my webserver which uses the following code to populate 3 labels. lblOne.Text = Page.User.Identity.Name...
4
by: James | last post by:
I have a VB windows forms application that accesses a Microsoft Access database that has been secured using user-level security. The application is being deployed using No-Touch deployment. The...
0
by: James | last post by:
I have a VB windows forms application that accesses a Microsoft Access database that has been secured using user-level security. The application is being deployed using No-Touch deployment. The...
1
by: Michael Howes | last post by:
I would think this would be very, very easy but in the 50 searches I've done I haven't found anything. If our application requires login and that user/password be a local windows account or more...
21
by: Dan Tallent | last post by:
In my application I have a form (Customer) that I want to be able to open multiple copies at once. Within this form I have other forms that can be opened. Example: ZipCode. When the user enters...
9
by: Scott Stark | last post by:
Hello, I'm *just* delving into Windows forms-based programming without the benefit of any books, etc. I have a background in light ASP.NET work, so forgive me if this is a really basic question...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.