我正在一个命令行应用程序中运行,该应用程序使用LogonUser
对用户进行身份验证。该函数正确返回,但正确失败(用户名或密码无效)。当我将 LogonUser
函数返回的 token 传递到 WindowsIdentity(IntPtr)
构造函数时,我收到错误:
Invalid token for impersonation - it cannot be duplicated.
我尝试使用 DuplicateToken
函数将 token 传递到 WindowsIdentity
构造函数之前复制 token 。这也失败了。我打开了 UAC 并且正在运行 Windows 7 x64。以管理员身份运行和以非管理员身份运行会产生相同的结果。
一些附加信息:
- 登录域
- 使用
LOGON32_LOGON_INTERACTIVE
- 使用
LOGON32_PROVIDER_DEFAULT
最佳答案
以下内容对您有用吗,还是会重现该问题?
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);
// ...
IntPtr token;
LogonUser(Username, Domain, Password, 8, 0, out token)
WindowsIdentity wi;
wi = new WindowsIdentity(token);
关于c# - WindowsIdentity 构造函数使用 LogonUser 的 token 引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5106349/