2017-04-24 47 views
0

我有一個Windows應用程序,它試圖驗證Active Directory上的用戶名/密碼與下面的代碼。PrincipalContext :: ValidateCredentials拋出LdapException與無效的密碼

PrincipalContext^ pc = gcnew PrincipalContext(ContextType::Domain); 
// validate the credentials 
bool isValid = pc->ValidateCredentials(userName, password); 
if(!isValid) 
{ 
    throw gcnew SecurityTokenValidationException("Invalid user ID/password"); 
} 

UserPrincipal^ upUser = UserPrincipal::FindByIdentity(pc, userName); 
if(upUser && !upUser->IsMemberOf(pc, IdentityType::SamAccountName, ADGroup)) 
{ 
    String^ msg = "User " + userName + " is not a member of the " + ADGroup + " group."; 
    throw gcnew SecurityTokenValidationException(msg); 
} 

當一個Visual Studio中運行該代碼2008/.NET 3.5/32位的身材,如果我給一個有效的用戶,但無效passwordd,ValidateCredentials()返回false。

使用的Visual Studio 2013/.NET 4.0/64位的身材,完全相同的代碼拋出LdapException:

System.DirectoryServices.Protocols.LdapException: The LDAP server is unavailable. 
at System.DirectoryServices.Protocols.LdapConnection.Connect() 
at System.DirectoryServices.Protocols.LdapConnection.BindHelper(NetworkCredential newCredential, Boolean needSetCredential) 
at System.DirectoryServices.AccountManagement.CredentialValidator.lockedLdapBind(LdapConnection current, NetworkCredential creds, ContextOptions contextOptions) 
at System.DirectoryServices.AccountManagement.CredentialValidator.BindLdap(NetworkCredential creds, ContextOptions contextOptions) 
at System.DirectoryServices.AccountManagement.CredentialValidator.Validate(String userName, String password) 
at System.DirectoryServices.AccountManagement.PrincipalContext.ValidateCredentials(String userName, String password) 
at soapcon.ADUserNameValidator.Validate(String userName, String password) 

如果我從網絡上斷開我的電腦,我得到一個PrincipalServerDownException例外,所以我我很確定我實際上正在和我們的AD服務器通話。

這是我的代碼的問題,是.NET的問題,還是可能是由於我們極其陳舊的Active Directory服務設置(Windows 2000)?

+0

這不是'C#''但C++ .net'。順便說一下,應用程序池是否在IIS配置中的域帳號下執行? IIS的行爲與Cassiny不同 – bradbury9

+0

@ bradbury9我沒有運行IIS,它只是一個Windows應用程序,在我的開發者域帳戶下運行。 –

+0

我不知道是否不同的框架會使用不同的默認身份驗證選項,您是否可以在兩種情況下嘗試'.ValidateCredentials(String,String,ContextOptions)'重載? – bradbury9

回答

1

我不知道是否不同的框架會使用不同的默認身份驗證選項,你可以在兩種情況下嘗試.ValidateCredentials (String, String, ContextOptions)重載嗎?

按照評論,試​​試這個ContextOptions標誌過載:

ContextOptions::Negotiate | ContextOptions::Signing | ContextOptions::Sealing 
相關問題