Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

logonuser (advapi32)
 
.
Summary
allows you to programmatically log a user on. Once done, the application space will take the identity of the newly logged on user.

C# Signature:

[DllImport("advapi32.dll")]
public static extern bool LogonUser(
    string lpszUsername,
    string lpszDomain,
    string lpszPassword,
    int dwLogonType,
    int dwLogonProvider,
    ref IntPtr phToken
    );

VB .NET Signature:

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

User-Defined Types:

None.

Notes:

It is very important to know which LOGON type you need. LOGON_NETWORK will allow access to network resources, while LOGON_INTERACTIVE will not. This will cause you hours of consteration if you don't pay attention to it.

To make this useful, you will also need to implement DuplicateToken.

Tips & Tricks:

Please add some!

Windows 2000 issue:

The LogonUser method works well with Windows XP, but has little value when using Windows 2000. There are security issues with Win 2000 that force you to grant the "Act as part of the Operating System" rights to each user that will invoke the method LogonUser. Since Microsoft advises against this we used the "CreateProcessWithLogonW" API instead of LogonUser. This starts another process on the local machine, runs an application of your choice in that process, and uses a valid UserID and Password passed to the method.

Sample Code:

const int LOGON32_LOGON_NETWORK = 3;
Int16 LOGON32_LOGON_INTERACTIVE  = 2;
Int16 LOGON32_PROVIDER_DEFAULT  = 0;

if (LogonUser(userName, domain, passWord, interactiveValue, LOGON32_PROVIDER_DEFAULT, ref token))
{
     if (DuplicateToken(token, 2, ref tokenDuplicate))
     {
     tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
     impersonationContext = tempWindowsIdentity.Impersonate();

     return impersonationContext == null;
     }
     else
     return false;
}
else
     return false;

Alternative Managed API:

TODO

Documentation
LogonUser on MSDN

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).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions