Declare Function LogonUser Lib "Advapi32" Alias "LogonUserA" (ByVal _ lpszUserName As String, ByVal lpszDomain As String, _ ByVal lpszPassword As String, ByVal dwLogonType As Long, _ ByVal dwLogonProvider As Long, phToken As Long) As Long
Функция LogonUser пытается зарегистрировать пользователя на локальном компьютере. Вы не можете использовать LogonUser на удаленном компьютере. Вы определяете пользователя при помощи имени пользователя (логин) и домена, и подтверждаете подлинность пользователя паролем. Если функция вызвана успешно, то вы получаете дескриптор к лексеме, которая представляет вошедшего пользователя. Вы можете тогда использовать этот дескриптор, чтобы исполнить роль указанного пользователя или, в большинстве случаев, создавать процесс, который выполняется в контексте указанного пользователя.
В успешном случае функция возвращает ненулевое значение. В случае ошибки функция возвращает 0 (для получения кода ошибки используйте GetLastError)
LOGON32_LOGON_BATCH | 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 LogonUser function does not cache credentials for this logon type. |
Const LOGON32_LOGON_INTERACTIVE = 2& | 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. |
Const LOGON32_LOGON_NETWORK = 3& | This logon type is intended for high performance servers to authenticate plaintext passwords. The LogonUser function does not cache credentials for this logon type. |
LOGON32_LOGON_NETWORK_CLEARTEXT | 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 LogonUser, verify that the user can access the system across the network, and still communicate with other servers. Windows NT: This value is not supported. |
LOGON32_LOGON_NEW_CREDENTIALS | 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. Windows NT: This value is not supported. |
LOGON32_LOGON_SERVICE | Indicates a service-type logon. The account provided must have the service privilege enabled. |
LOGON32_LOGON_UNLOCK | 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. |
Const LOGON32_PROVIDER_DEFAULT = 0& | Use the standard logon provider for the system. The default security provider is negotiate, unless you pass NULL for the domain name and the user name is not in UPN format. In this case, the default provider is NTLM. Windows 2000/NT: The default security provider is NTLM. |
LOGON32_PROVIDER_WINNT50 | Use the negotiate logon provider. Windows NT: This value is not supported. |
LOGON32_PROVIDER_WINNT40 | Use the NTLM logon provider. |
LOGON32_PROVIDER_WINNT35 | Use the Windows NT 3.5 logon provider. |
' Проверяем логин и пароль пользователя Private Function CheckWindowsUser(ByVal UserName As String, _ ByVal Password As String, Optional ByVal Domain As String) As Boolean Dim hToken As Long, ret As Long ' если домен не используется If Len(Domain) = 0 Then Domain = vbNullString ' проверяем логин/пароль ret = LogonUser(UserName, Domain, Password, LOGON32_LOGON_INTERACTIVE, _ LOGON32_PROVIDER_DEFAULT, hToken) ' если ненулевое значение, значит ошибки нет If ret Then CheckWindowsUser = True CloseHandle hToken End If End Function Private Sub Command1_Click() MsgBox CheckWindowsUser(Text1.Text, Text2.Text) End Sub
CloseHandle, CreateProcessAsUser