使用 JNA 的 LogonUser 导致 "unknown user name or bad password"

LogonUser with JNA leads to "unknown user name or bad password"

我尝试实现一项功能,以使用特定凭据执行某些应用程序。为此,我检查是否可以使用凭据登录。

import com.sun.jna.LastErrorException;
import com.sun.jna.platform.win32.Advapi32;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinBase;
import com.sun.jna.platform.win32.WinNT;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class TestHarness {

    public static void main(String[] args) throws UnknownHostException {

        WinNT.HANDLEByReference phUser = new WinNT.HANDLEByReference();
        System.out.println(InetAddress.getLocalHost().getHostName());
        if (!Advapi32.INSTANCE.LogonUser("de313e", ".",
                "password", WinBase.LOGON32_LOGON_NETWORK, WinBase.LOGON32_PROVIDER_DEFAULT, phUser)) {
            throw new LastErrorException(Kernel32.INSTANCE.GetLastError());
        }
    }
}

不幸的是,这给了我

com.sun.jna.LastErrorException: GetLastError() returned 1326

提供的用户名是我当前的用户名。为什么这不起作用?

我是 运行 它在 Windows 10。我的密码包含一些特殊字符,如 !&。用户名为:

LogonUser 的文档说明参数 lpszDomain:

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.

由于您使用的是 ".",因此您仅针对本地用户数据库进行验证。这适用于本地计算机 (NB3DE2730054) 上的 sysadmin 帐户。但是,您正在尝试验证域用户 de313e,因此您必须指定域 MASTDOM

作为替代方案,您可以将域设置为 null 并将域包含在用户中:de313e@MASTDOM。或者,正如您在评论中指出的那样,如果您使用 logonType LOGON32_LOGON_NEW_CREDENTIALS 和 logonProvider LOGON32_PROVIDER_WINNT50,则 "." 将起作用。