首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >返回false的ValidateCredentials条件

返回false的ValidateCredentials条件
EN

Stack Overflow用户
提问于 2020-05-15 15:15:15
回答 1查看 421关注 0票数 0

我使用原则上下文的ValidateCredentials方法对Active Directory进行身份验证,但由于某种原因,它返回false,尽管密码正确且未过期。我已经用UserPrincipal检查了用户的状态,但是尽管用户被启用而没有锁定,它仍然返回false。我还确保最后一个密码集没有超过其过期日期。

是否还有其他因素使ValidateCredentials返回错误?

所用代码:

代码语言:javascript
复制
bool expired = false;
bool blocked = false;
bool disabled = false;
int failedLogins;

Resultado resul = new Resultado();
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, Config.ActiveDirectory.Server,
                                                                            Directory,
                                                                            Config.ActiveDirectory.User,
                                                                            Config.ActiveDirectory.Pass);
UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, userName);

if (user != null)
{
    disabled = (user.Enabled == null || !user.Enabled.Value);
    blocked = user.IsAccountLockedOut();
    expired = user.LastPasswordSet == null || DateTime.UtcNow.Subtract(user.LastPasswordSet.Value).Days >= Config.ActiveDirectory.MaxPassAge;

    failedLogins = user.BadLogonCount; 


    if (domainContext.ValidateCredentials(userName, password))
        resul.ResultCode = 0;
    else
    {
        user = UserPrincipal.FindByIdentity(domainContext, userName);

        if (disabled)
            resul.ResultCode = 7;
        else if (blocked)
            resul.ResultCode = 3;
        else if (failedLogins != user.BadLogonCount)
            resul.ResultCode = 2;
        else
            resul.ResultCode = 4;
    }
}
else
    resul.ResultCode = 1;
return resul;
EN

回答 1

Stack Overflow用户

发布于 2020-05-15 15:33:42

还可以使用AccountExpirationDate属性检查帐户过期日期。

另外,检查密码是否必须在下一次登录时更改。如果是这样的话,LastPasswordSet属性将是null

documentation for ValidateCredentials中也有一个可能相关的说明:

-- userName参数必须采用用户名形式(例如,mcampbell),而不是域\ username或username@userName

你试过用这个用户名和密码登录计算机吗?这是判断ValidateCredentials是否对你撒谎的最佳方法。

好奇:为什么在您的user块中再次将else设置为相同的东西?这似乎没有必要。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61822552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档