【问题标题】:PrincipalContext.ValidateCredentials: find username or password is invalidPrincipalContext.ValidateCredentials:发现用户名或密码无效
【发布时间】:2017-11-23 05:22:22
【问题描述】:

我在我的应用程序中使用 AD 身份验证:

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

有什么方法可以查明我是否因为用户名或密码无效而将isValid 设置为false

【问题讨论】:

    标签: c# active-directory principalcontext


    【解决方案1】:

    你不能直接确定哪一个是无效的。但是您可以尝试从活动目录中检索用户,以确定在这样的错误验证后哪个是错误的;

        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
                }
            }
        }
    

    【讨论】:

    • 我的问题与为什么我的用户名无效无关。我想知道其中 哪些 是无效的。我的意思是,我在测试时就知道了 - 我想知道是否有任何方法可以通过代码找到它。
    • user == null 对有效的用户名和无效的密码为真。
    • 我不知道您的活动目录上发生了什么。但是您应该尝试从 AD 中拉出用户来检查它。这是找出用户名或密码无效的唯一方法。
    猜你喜欢
    • 2015-05-31
    • 2016-08-22
    • 2016-11-14
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多