【问题标题】:Determine AD password policy programmatically以编程方式确定 AD 密码策略
【发布时间】:2011-02-14 13:06:32
【问题描述】:

我一直在使用System.DirectoryService (ADSI) 类和方法在 Active Directory 中创建和更改用户。

最近我们添加了一项功能,允许用户通过系统设置自己的密码。但是,当密码策略集不接受密码时,使用SetPassword 方法会引发异常。

userEntry.Invoke("SetPassword", new object[] {password});

我的问题是:在尝试使用 SetPassword 方法之前,如何检查密码是否符合密码策略?

我在this post 中读到,您可以从根域节点获取密码策略设置,但我在哪里可以了解有关每个属性含义的更多信息?例如,要满足“复杂性”政策需要哪些字符?

一旦我知道这一点,我就可以实现自己的密码检查方法,但由于这是一种容易出错的方法,我宁愿使用内置检查并为用户提供有关密码错误的适当信息.

【问题讨论】:

标签: c# active-directory


【解决方案1】:

我正在从事一个类似的项目。我们正在推出忘记密码的应用程序。我最终只是做了一个Invoke("SetPassword", "[randomString]") 并为Invoke("ChangePassword","[randomString]","[user supplied pw]") 保存了随机字符串。 ChangePassword 的结果返回给用户。

SetPassword 不检查密码复杂性或历史规则。这与在 AD 中右键单击用户并选择“重置密码”相同。但是,ChangePassword 会检查密码历史记录要求。

【讨论】:

    【解决方案2】:

    复杂性策略是它必须包含以下五种类型中的三种:

    • 大写字母
    • 小写字母
    • 数字
    • 非字母数字字符:~!@#$%^&*_-+=`|(){}[]:;"',.?/
    • Unicode 字符是字母但不是大写或小写。

    它也不能是 sAMAccountName 或 displayName(或其中的一部分)。你可以阅读它here。其他密码策略规则在adjacent documents

    您可以尝试设置它并捕获异常,但从记忆中我认为它不会告诉您密码有什么问题,只是它不符合要求。

    【讨论】:

      【解决方案3】:

      你想要的API函数是NetValidatePasswordPolicy

      它有三种运行模式:

      • NetValidateAuthentication:如果您正在验证用户;因此该功能可以检查密码过期策略、错误登录尝试、帐户锁定、错误登录尝试等
      • NetValidatePasswordChange:如果用户正在更改密码;因此该函数可以检查锁定或密码策略

      以及你想要的模式:

      • NetValidatePasswordReset:您是正在重置用户密码的管理员;仅检查密码复杂性。

      我将尝试即时从另一种语言转码为 C#;但你必须 P/Invoke 它。

      <summary>Check password during password reset.
      
          The result from NetValidatePasswordReset, this member can be one of the following values.
      
              NERR_Success                        The password passes the validation check.
              NERR_PasswordTooShort           Validation failed. The password does not meet policy requirements because it is too short.
              NERR_PasswordTooLong                Validation failed. The password does not meet policy requirements because it is too long.
              NERR_PasswordNotComplexEnough   Validation failed. The password does not meet policy requirements because it is not complex enough.
              NERR_PasswordFilterError        Validation failed. The password does not meet the requirements of the password filter DLL.
      </summary>
      UInt32 TestPasswordComplexity(String username, SecureString password)
      {
         //All code on stack overflow is in the public domain; no attribution
         //is required.
         const UInt32 NetValidatePasswordReset = 3;
      
         NET_VALIDATE_PASSWORD_RESET_INPUT_ARG args = new NET_VALIDATE_PASSWORD_RESET_INPUT_ARG();
         args.UserAccountName = Username; //some policies check that your password cannot contain your username
         args.ClearPassword = SecureStringToString(password);
      
         PNET_VALIDATE_OUTPUT_ARG res;
      
         DWORD le = NetValidatePasswordPolicy(null, null, NetValidatePasswordReset, @args, {out}Pointer(res));
      
         if (le <> NERR_Success)
            throw new WindowsException(le); //
      
         return res.ValidationStatus;
      }
      

      【讨论】:

        猜你喜欢
        • 2021-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-18
        相关资源
        最近更新 更多