c# - WindowsIdentity 构造函数使用 LogonUser 的 token 引发异常

标签 c# pinvoke windows-identity

我正在一个命令行应用程序中运行,该应用程序使用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/

相关文章:

c# - 当您从 C# 进行 P/调用时,异步过程调用如何处理编码(marshal)的委托(delegate)?

c# - P/Invoke 动态 DLL 搜索路径

c# - 如何从 C++/C# 调用此 DLL 函数

c# - 无论谁登录,WindowsIdentity 总是返回相同的用户

c# - Windows 8 XAML 对象和快照 View

c# - 从 EFCore 延迟加载上下文中删除导航集合实体 - 导航集合未立即更新

c# - 远程服务模拟显示用户 'NT Authority\Anonymous Logon' 登录失败

wif - 消息 : ID4243: Could not create a SecurityToken. token 缓存中未找到 token ,上下文中未找到 cookie

c# - C++ 调用 Java 代码与 C# 代码的性能

c# - 如何使用 MongoDb C# 规范存储文件