Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than advapi32, prefix the name with the module name and a period.
One important thing to note is that in order to use WindowsIdentity.Impersonate(), we need a primary token, so if we go with LOGON32, we'll need to use DuplicateHandle in order to get at the primary token. Since I like to avoid extra work like that, it looks like LOGON32 or LOGON32 would both be more appropriate choices. Selecting between them will depend on the rights that the account we're trying to logon has.
Once your call to LogonUser has gotten you a user token for the user you'd like to impersonate, you can then call WindowsIdentity.Impersonate() and have your thread take over the identity of the Windows user you just logged on.
Tips & Tricks:
Please add some!
Sample Code:
// Call LogonUser to get a token for the user
IntPtr userHandle = IntPtr.Zero();
bool loggedOn = LogonUser(
user,
domain,
password,
LogonType.Interactive,
LogonProvider.Default,
out userHandle);
if(!loggedOn)
throw new Win32Exception(Marshal.GetLastWin32Error());
// Begin impersonating the user
WindowsImpersonationContext impersonationContext = WindowsIdentity.Impersonate(userHandle.Token);
DoSomeWorkWhileImpersonating();
// Clean up
CloseHandle(userHandle);
impersonationContext.Undo();
Alternative Managed API:
Do you know one? Please contribute it!
The DuplicateHandle API
12/18/2008 8:08:02 AM - -82.137.167.2
The LogonUser function attempts to log a user on to the local computer. The local computer is the computer from which LogonUser was called. You cannot use LogonUser to log on to a remote computer.
12/17/2022 3:26:24 AM - JM-163.116.163.139
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).