加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 当“用户必须在下次登录时更改密码”时,LDAP验证失败.有解

发布时间:2020-12-16 01:43:10 所属栏目:百科 来源:网络整理
导读:当设置“用户必须在下次登录时更改密码”时,我在用户验证时遇到问题. 以下是我验证用户的方法: Boolean ValidateUser(String userName,String password){ try { var userOk = new DirectoryEntry("LDAP://my LDAP server",userName,password,Authentication
当设置“用户必须在下次登录时更改密码”时,我在用户验证时遇到问题.

以下是我验证用户的方法:

Boolean ValidateUser(String userName,String password)
{
    try
    {
        var userOk = new DirectoryEntry("LDAP://<my LDAP server>",userName,password,AuthenticationTypes.Secure 
                                      | AuthenticationTypes.ServerBind);
        return true;
    }
    catch (COMException ex)
    {
        if (ex.ErrorCode == -2147023570) // 0x8007052E -- Wrong user or password
            return false;
        else
            throw;
    }
}

设置“必须更改密码”时,将按预期捕获COMException,但是,ErrorCode与密码错误时相同.

有谁知道如何解决这一问题?

我需要一个返回代码,告诉密码是正确的,并且用户必须更改密码.

我不想在C#中实现Kerberos只是为了在用户必须更改密码时检查该死的标志.

解决方法

经过长时间的互联网搜索,一些带有错误信息的实证工作和一些通过Win32API进行探索,我找到了一个迄今为止有效的解决方案.

Boolean ValidateUser(String userName,String password)
{
  try
  {
    var user = new DirectoryEntry("LDAP://<my LDAP server>",password);
    var obj = user.NativeObject;
    return true;
  }
  catch (DirectoryServicesCOMException ex)
  {
    /*
     * The string " 773," was discovered empirically and it is related to the
     * ERROR_PASSWORD_MUST_CHANGE = 0x773 that is returned by the LogonUser API.
     * 
     * However this error code is not in any value field of the 
     * error message,therefore we need to check for the existence of 
     * the string in the error message.
     */
     if (ex.ExtendedErrorMessage.Contains(" 773,"))
        throw new UserMustChangePasswordException();

     return false;
  }
  catch
  {
     throw;
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读