LogonUser function

The LogonUser function attempts to log a user on to the local computer. The function authenticates the user, and, if successful, returns a handle to an access token that represents the logged-on user.

Syntax

BOOL LogonUserA(
  LPCSTR  lpszUsername,
  LPCSTR  lpszDomain,
  LPCSTR  lpszPassword,
  DWORD   dwLogonType,
  DWORD   dwLogonProvider,
  PHANDLE phToken
);

BOOL LogonUserW(
  LPCWSTR lpszUsername,
  LPCWSTR lpszDomain,
  LPCWSTR lpszPassword,
  DWORD   dwLogonType,
  DWORD   dwLogonProvider,
  PHANDLE phToken
);

Parameters

NameTypeDescription
lpszUsernameLPCSTR/WPointer to a null-terminated string that specifies the name of the user.
lpszDomainLPCSTR/WPointer to a null-terminated string that specifies the domain or local computer name.
lpszPasswordLPCSTR/WPointer to a null-terminated string that specifies the password.
dwLogonTypeDWORDType of logon operation. See table below.
dwLogonProviderDWORDLogon provider. Usually LOGON32_PROVIDER_DEFAULT.
phTokenPHANDLEPointer to a handle that receives the token.

Logon type values

ConstantValueMeaning
LOGON32_LOGON_INTERACTIVE2Logon by user with interactive logon rights.
LOGON32_LOGON_NETWORK3Network logon, no profile loaded.
LOGON32_LOGON_BATCH4Batch logon for scheduled tasks.
LOGON32_LOGON_SERVICE5Logon for service accounts.
LOGON32_LOGON_UNLOCK7Unlock workstation.

Return value

If the function succeeds, the return value is non‑zero. If it fails, the return value is zero. Call GetLastError for extended error information.

Remarks

Example

#include <windows.h>
#include <stdio.h>

int main()
{
    HANDLE hToken = NULL;
    BOOL  ok = LogonUserA(
        "UserName",       // user name
        "Domain",         // domain or local computer
        "Password123",   // password
        LOGON32_LOGON_INTERACTIVE,
        LOGON32_PROVIDER_DEFAULT,
        &hToken);

    if (ok) {
        printf("Logon successful. Token: %p\n", hToken);
        CloseHandle(hToken);
    } else {
        printf("Logon failed. Error: %lu\\n", GetLastError());
    }
    return 0;
}

See also