【问题标题】:Detect if User Must Reset Password In Active Directory Using C#检测用户是否必须使用 C# 在 Active Directory 中重置密码
【发布时间】:2011-11-08 15:46:26
【问题描述】:

在 Active Directory 中,如果用户的帐户被禁用然后启用,默认情况下,用户必须在首次登录时更改其密码。我正在努力使用 C# 以编程方式检测到这一点?如果用户必须重置他们的属性,是否有设置的属性或类似的东西?

假设我有一个指向用户的 DirecotryEntry 对象:

DirectoryEntry user = ...

有没有我可以使用的属性:

user.Properties[someProperty];

【问题讨论】:

    标签: c# active-directory


    【解决方案1】:

    条件存储在两个属性中:

    • pwdLastSet : 如果值设置为 0 ...
    • userAccountControl : 并且未设置 UF_DONT_EXPIRE_PASSWD 标志。

    来自here

    【讨论】:

      【解决方案2】:

      这是我为此而写的。不完全回答您的问题,但对以后阅读它的其他人有用。

      重要的部分来自于 PrincipalContext。 以上所有内容都是我尝试始终以正确的大写形式恢复 AdName 的方式。

      请注意,这只是第一个答案的代码,使用用户主体而不是 DE 测试 LastPasswordSet。

      埃里克-

           private bool TestAdShouldChangePassword( string adUser )
           {
                          try
                          {
                              string adName = "";
                              MembershipUser mu = Membership.GetUser( adUser );
      
                              if ( mu != null )
                              {
                                  IStudentPortalLoginBLL splBll = ObjectFactory.GetInstance< IStudentPortalLoginBLL >();
                                  adName = splBll.GetCleanAdName( adUser );// I wrote this is just pulls outhe name and fixes the caplitalization - EWB
      
                                  PrincipalContext pctx = new PrincipalContext( System.DirectoryServices.AccountManagement.ContextType.Domain );
                                  UserPrincipal p = UserPrincipal.FindByIdentity( pctx, adName );
      
                                  if ( p == null )
                                      return false;
      
                                  if ( p.LastPasswordSet.HasValue == false && p.PasswordNeverExpires == false )
                                  {
                                      return true;
                                  }
                              }
                          }
                          catch ( MultipleMatchesException mmex )
                          {
                              log.Error ( "TestAdShouldChangePassword( ad user = '" + adUser + "' ) - Exception finding user, can't determine if ad says to change password, returing false : Ex = " + mmex.ToString() );
                          }
      
                          return false;
            }
      

      【讨论】:

      • 太棒了。我现在要试试这个。但问题是,这使用了哪些参考资料?
      • System.DirectoryServices.AccountManagement;使用 System.Security.Authentication;使用 System.Web.Security;我不记得它真正需要哪些,但我最好的猜测是 S.DS.AM
      【解决方案3】:

      能够使用以下代码获得它:

      public bool PasswordRequiresChanged(string userName) { DirectoryEntry user = GetUser(userName); //A directory entry pointing to the user Int64 pls; int uac; if (user != null && user.Properties["pwdLastSet"] != null && user.Properties["pwdLastSet"].Value != null) { pls = ConvertADSLargeIntegerToInt64(user.Properties["pwdLastSet"].Value); } else { throw new Exception("Could not determine if password needs reset"); } if (user != null && user.Properties["UserAccountControl"] != null && user.Properties["UserAccountControl"].Value != null) { uac = (int)user.Properties["UserAccountControl"].Value; } else { throw new Exception("Could not determine if password needs reset"); } return (pls == 0) && ((uac & 0x00010000) == 0) ? true : false; } private static Int64 ConvertADSLargeIntegerToInt64(object adsLargeInteger) { var highPart = (Int32)adsLargeInteger.GetType().InvokeMember("HighPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null); var lowPart = (Int32)adsLargeInteger.GetType().InvokeMember("LowPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null); return highPart * ((Int64)UInt32.MaxValue + 1) + lowPart; }

      【讨论】:

        【解决方案4】:
        var username = "radmin";
        var adContext = new PrincipalContext(ContextType.Domain, adLocation, adContainer, adAdminUsername, adAdminPassword);
        var user = UserPrincipal.FindByIdentity(adContext, username);
        Console.WriteLine(user.LastPasswordSet);
        

        如果 LastPasswordSet 的值为空,则“用户必须在下次登录时更改密码”。

        【讨论】:

        • 我希望用户下次登录时不要更改密码How-to?
        猜你喜欢
        • 1970-01-01
        • 2018-12-06
        • 1970-01-01
        • 2023-03-30
        • 1970-01-01
        • 2010-09-28
        • 2014-09-10
        • 2021-10-09
        • 1970-01-01
        相关资源
        最近更新 更多