簡體   English   中英

PrincipalContext.ValidateCredentials:查找用戶名或密碼無效

[英]PrincipalContext.ValidateCredentials: find username or password is invalid

我在我的應用程序中使用AD身份驗證:

 bool _isValid;
 using (var pc = new PrincipalContext(ContextType.Domain, DomainPath))
 {
     isValid = pc.ValidateCredentials(username, password, ContextOptions.Negotiate);
 }

有什么方法isValid由於用戶名無效或密碼無效而將isValid設置為false嗎?

您無法直接確定哪一個無效。 但是您可以嘗試從活動目錄中檢索用戶,以確定經過這樣的錯誤驗證后哪個用戶錯了。

    bool _isValid;
    using (var pc = new PrincipalContext(ContextType.Domain, DomainPath))
    {
        isValid = pc.ValidateCredentials(username, password, ContextOptions.Negotiate);
        if (!isValid)
        {
            var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, username);
            if (user == null)
            {
                //User doesn't exist
            }
            else
            {
                //Password is invalid
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM