我在這裏看到一些奇怪的行爲,使用PrincipalContext.ValidateCredentials
。該設置是父/子設置中的兩個Active Directory域(因此我們有主域company.com
和子域development.company.com
)。ValidateCredentials對未知用戶返回true?
當我根據主域驗證憑據時,ValidateCredentials
的行爲與預期的一樣,對於良好的用戶/傳遞對返回true,對其他任何情況返回false。
但是,如果我驗證子域中的用戶,則ValidateCredentials
對於良好的用戶名/密碼和無效用戶均返回true。如果我向有效的用戶提供了無效的密碼,它會正確地返回false。
現在我現在正在通過首先執行UserPrincipal.FindByIdentity()
來解決此問題,如果用戶存在,然後調用ValidateCredentials
- 但我想了解發生了什麼。
另一個解決方法我看是通過傳遞用戶名作爲domain\username
爲MSDN entry for ValidateCredentials
states:
在此功能中的每一個版本,用戶名字符串可以在 一個多種不同格式。有關可接受的 格式類型的完整列表,請參閱ADS_NAME_TYPE_ENUM文檔。
...其中列出了這種用戶名形式。但是,這會導致ValidateCredentials
總是返回true,不管我通過什麼樣的用戶名和密碼的組合
相關的代碼是:
bool authenticated = false;
// Various options tried for ContextOptions, [etc] inc. explicit username/password to bind to AD with -- no luck.
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain, null, ContextOptions.Negotiate, null, null))
{
log(pc.ConnectedServer + " => " + pc.UserName + " => " + pc.Name + " => " + pc.Container);
using (var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, username))
{
if (user != null)
{
log(user.DistinguishedName + "; " + user.DisplayName);
authenticated = pc.ValidateCredentials(username, password);
} else {
log("User not found");
// Debug only -- is FindByIdentity() needed. This should always return
// false, but doesn't.
authenticated = pc.ValidateCredentials(username, password);
}
}
}
return authenticated;
任何及所有(合理)建議表示歡迎 - 我。由於它違背了所有的期望,我對此感到頭疼。
我應該補充一點:這是在我的機器上運行,兩者都是主域的成員。不過,我也嘗試在我的機器上作爲子域的用戶(runas /user:subdomain\user cmd
)在命令提示符下運行它,並得到完全相同的結果。
我有兩個域。我可以使用我的域的IP地址來調用驗證憑據,並且如果我只是傳遞來自任一域的用戶的用戶名或密碼,它就會成功。我有點驚訝,我不必提供這些額外的信息,並且有點困惑,然後我會如何確定我是否從正確的域名驗證了正確的用戶。 (不可能每個人都說一個jsmith?) – Greg