2

我在一个命令行应用程序中运行,该应用程序使用LogonUser. 该函数正确返回并正确失败(用户名或密码无效)。当我将LogonUser函数返回的令牌传递给WindowsIdentity(IntPtr)构造函数时,我收到错误:

用于模拟的令牌无效 - 它不能被复制。

WindowsIdentity在使用该函数将令牌传递给构造函数之前,我尝试过复制令牌DuplicateToken。这也失败了。我有 UAC 并且正在运行 Windows 7 x64。以管理员和非管理员身份运行会产生相同的结果。

一些附加信息:

  • 登录到域
  • 使用LOGON32_LOGON_INTERACTIVE
  • 使用LOGON32_PROVIDER_DEFAULT
4

3 回答 3

1

以下内容对您有用,还是重现问题?

[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);
于 2011-02-24T15:30:48.383 回答
1

我只在使用 .Net Framework 4 编译的代码中遇到了同样的错误。使用所有以前的版本编译时没有错误。

此代码曾经在 .net 4 中失败:

using(WindowsIdentity identity = new WindowsIdentity(accessToken))
    context = identity.Impersonate();

但是,我发现这有效:

context = WindowsIdentity.Impersonate(accessToken);
于 2012-03-27T15:30:02.890 回答
1

这最终成为环境。尝试对域进行身份验证时出现 DNS 问题。重置开发盒解决了这个问题。

于 2011-03-01T16:31:20.173 回答