2013-02-20 43 views
2

我有以下代碼來驗證Active Directory用戶(在Win7上的VS2008)。根據AD異常驗證用戶名和密碼

try 
{ 
    PrincipalContext pc = new PrincipalContext(ContextType.Domain, strDomainName)) 
    isPass = pc.ValidateCredentials(strDomainUserName, strDomainUserPass); 
} 
catch (Exception ex) 
{ 
    Logger.Log("authentiate domain user fail: {0},{1}", ex.ToString(), strDomainUserLoginName); 
} 

某些廣告的用戶,這是確定的,但對於AD用戶,我會出現以下情況例外: System.DirectoryServices.Protocols.DirectoryOperationException:The server cannot handle directory requests.

System.DirectoryServices.Protocols.ErrorChecking.CheckAndSetLdapError(Int32 error) 

System.DirectoryServices.Protocols.LdapSessionOptions.FastConcurrentBind() 

System.DirectoryServices.AccountManagement.CredentialValidator.BindLdap(NetworkCredential creds, ContextOptions contextOptions) 

System.DirectoryServices.AccountManagement.CredentialValidator.Validate(String userName, String password) 

System.DirectoryServices.AccountManagement.PrincipalContext.ValidateCredentials(String userName, String password) 

因此,沒有Active Directory域用戶設置會造成這樣的例外?

+0

看看這篇文章的解決方案http://stackoverflow.com/questions/2473222/validatecredentials-returns-false-on-first-call-but-true-on-subsequent-calls – MethodMan 2013-02-20 09:03:34

+0

你也可以看看這個Stackoverflow發佈我相信它會回答你的問題.. http://stackoverflow.com/questions/9473314/active-directory-principalcontext-validatecredentials-domain-disambiguation – MethodMan 2013-02-20 09:04:54

回答

1

對不起,我的描述是錯誤的。實際上,所有AD用戶驗證都會得到這樣的例外。

而且工作環境是: (1)如果驗證碼正好在AD機器上運行,那麼代碼就會得到這樣的例外。 (2)否則,不存在這樣的例外。

爲ValidateCredentials附加ContextOptions.Negotiate參數以解決問題。