The LogonUserEx function attempts to log a user on to the local computer. The local computer is the computer from which LogonUserEx was called. You cannot use LogonUserEx to log on to a remote computer. You specify the user with a user name and domain and authenticate the user with a plaintext password. If the function succeeds, you receive a handle to a token that represents the logged-on user. You can then use this token handle to impersonate the specified user or, in most cases, to create a process that runs in the context of the specified user.
BOOL LogonUserEx( _In_ LPTSTR lpszUsername, _In_opt_ LPTSTR lpszDomain, _In_opt_ LPTSTR lpszPassword, _In_ DWORD dwLogonType, _In_ DWORD dwLogonProvider, _Out_opt_ PHANDLE phToken, _Out_opt_ PSID *ppLogonSid, _Out_opt_ PVOID *ppProfileBuffer, _Out_opt_ LPDWORD pdwProfileLength, _Out_opt_ PQUOTA_LIMITS pQuotaLimits );
- lpszUsername [in]
A pointer to a null-terminated string that specifies the name of the user. This is the name of the user account to log on to. If you use the user principal name (UPN) format, user@DNS_domain_name, the lpszDomain parameter must be NULL.
- lpszDomain [in, optional]
A pointer to a null-terminated string that specifies the name of the domain or server whose account database contains the lpszUsername account. If this parameter is NULL, the user name must be specified in UPN format. If this parameter is ".", the function validates the account by using only the local account database.
- lpszPassword [in, optional]
A pointer to a null-terminated string that specifies the plaintext password for the user account specified by lpszUsername. When you have finished using the password, clear the password from memory by calling the SecureZeroMemory function. For more information about protecting passwords, see Handling Passwords.
- dwLogonType [in]
The type of logon operation to perform. This parameter can be one of the following values.
This logon type is intended for batch servers, where processes may be executing on behalf of a user without their direct intervention. This type is also for higher performance servers that process many plaintext authentication attempts at a time, such as mail or web servers. The LogonUserEx function does not cache credentials for this logon type.
This logon type is intended for users who will be interactively using the computer, such as a user being logged on by a terminal server, remote shell, or similar process. This logon type has the additional expense of caching logon information for disconnected operations; therefore, it is inappropriate for some client/server applications, such as a mail server.
This logon type is intended for high performance servers to authenticate plaintext passwords. The LogonUserEx function does not cache credentials for this logon type.
This logon type preserves the name and password in the authentication package, which allows the server to make connections to other network servers while impersonating the client. A server can accept plaintext credentials from a client, call LogonUserEx, verify that the user can access the system across the network, and still communicate with other servers.
This logon type allows the caller to clone its current token and specify new credentials for outbound connections. The new logon session has the same local identifier but uses different credentials for other network connections.
This logon type is supported only by the LOGON32_PROVIDER_WINNT50 logon provider.
Indicates a service-type logon. The account provided must have the service privilege enabled.
This logon type is for GINA DLLs that log on users who will be interactively using the computer. This logon type can generate a unique audit record that shows when the workstation was unlocked.
- dwLogonProvider [in]
The logon provider. This parameter can be one of the following values.
Use the standard logon provider for the system. The default security provider is NTLM.
Use the negotiate logon provider.
Use the NTLM logon provider.
- phToken [out, optional]
A pointer to a handle variable that receives a handle to a token that represents the specified user.
You can use the returned handle in calls to the ImpersonateLoggedOnUser function.
In most cases, the returned handle is a primary token that you can use in calls to the CreateProcessAsUser function. However, if you specify the LOGON32_LOGON_NETWORK flag, LogonUserEx returns an impersonation token that you cannot use in CreateProcessAsUser unless you call DuplicateTokenEx to convert the impersonation token to a primary token.
When you no longer need this handle, close it by calling the CloseHandle function.
- ppLogonSid [out, optional]
A pointer to a pointer to a security identifier (SID) that receives the SID of the user logged on.
When you have finished using the SID, free it by calling the LocalFree function.
- ppProfileBuffer [out, optional]
A pointer to a pointer that receives the address of a buffer that contains the logged on user's profile.
- pdwProfileLength [out, optional]
A pointer to a DWORD that receives the length of the profile buffer.
- pQuotaLimits [out, optional]
A pointer to a QUOTA_LIMITS structure that receives information about the quotas for the logged on user.
If the function succeeds, the function returns nonzero.
If the function fails, it returns zero. To get extended error information, call GetLastError.
The LOGON32_LOGON_NETWORK logon type is fastest, but it has the following limitations:
- The function returns an impersonation token, not a primary token. You cannot use this token directly in the CreateProcessAsUser function. However, you can call the DuplicateTokenEx function to convert the token to a primary token, and then use it in CreateProcessAsUser.
- If you convert the token to a primary token and use it in CreateProcessAsUser to start a process, the new process cannot access other network resources, such as remote servers or printers, through the redirector. An exception is that if the network resource is not access controlled, then the new process will be able to access it.
The SE_TCB_NAME privilege is not required for this function unless you are logging onto a Passport account.
The account specified by lpszUsername must have the necessary account rights. For example, to log on a user with the LOGON32_LOGON_INTERACTIVE flag, the user (or a group to which the user belongs) must have the SE_INTERACTIVE_LOGON_NAME account right. For a list of the account rights that affect the various logon operations, see Account Object Access Rights.
A user is considered logged on if at least one token exists. If you call CreateProcessAsUser and then close the token, the user is still logged on until the process (and all child processes) have ended.
If the LogonUserEx call is successful, the system notifies network providers that the logon occurred by calling the provider's NPLogonNotify entry-point function.
Minimum supported client
Windows XP [desktop apps only]
Minimum supported server
Windows Server 2003 [desktop apps only]
Unicode and ANSI names
LogonUserExW (Unicode) and LogonUserExA (ANSI)
- Client/Server Access Control
- Client/Server Access Control Functions