2011-02-24 46 views
2

我正在使用LogonUser驗證用戶的命令行應用程序中運行。該功能正確返回並且正確(用戶名或密碼無效)失敗。當我路過的LogonUser功能到WindowsIdentity(IntPtr)構造函數返回的令牌,我收到錯誤:WindowsIdentity構造函數拋出來自LogonUser令牌的異常

Invalid token for impersonation - it cannot be duplicated.

我試過它傳遞到使用DuplicateToken功能WindowsIdentity構造函數之前複製令牌。這也失敗了。我有UAC,並且正在運行Windows 7 x64。以管理員身份和非管理員身份運行會產生相同的結果。

一些產生額外的信息:

  • 登錄到域
  • 使用LOGON32_LOGON_INTERACTIVE
  • 使用LOGON32_PROVIDER_DEFAULT

回答

1

這最終成爲環境問題。嘗試對域進行身份驗證時出現DNS問題。重置開發盒會解決問題。

+0

我也遇到過這種情況,這有助於解決問題。在Windows日誌 - >系統下的事件查看器中,我也遇到了奇怪的SChannel錯誤。重啓後固定出現。 – jordanhill123 2014-05-15 00:35:44

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); 
+0

它返回相同的錯誤:| – 2011-02-24 15:37:51

1

我只在使用.Net Framework 4編譯的代碼中出現了相同的錯誤。使用所有先前版本編譯時沒有錯誤。

使用.NET 4的失敗驗證碼:

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

然而,我發現,這個工程:

context = WindowsIdentity.Impersonate(accessToken); 
+0

這個建議對我沒有任何影響;仍然有同樣的例外。 – atconway 2012-10-31 19:52:19

相關問題