检测用户是否必须使用C#在Active Directory中重置密码

本文关键字:Directory Active 密码 是否 用户 检测 | 更新日期: 2023-09-27 18:14:03

在Active Directory中,如果用户的帐户被禁用然后启用,默认情况下,用户必须在首次登录时更改密码。我很难用C#程序检测到这一点?如果用户必须重置其属性,是否有设置的属性或类似的内容?

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

DirectoryEntry user = ...

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

user.Properties[someProperty];

检测用户是否必须使用C#在Active Directory中重置密码

条件存储在两个属性中:

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

从这里开始。

以下是我为此所写的内容。不完全回答你的问题,但对稍后阅读的其他人有用。

重要的位来自上的PrincipalContext。上面的所有内容都是我试图让AdName始终以正确的大写字母返回的方式。

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

Eric-

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

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


        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;
        }
var username = "radmin";
var adContext = new PrincipalContext(ContextType.Domain, adLocation, adContainer, adAdminUsername, adAdminPassword);
var user = UserPrincipal.FindByIdentity(adContext, username);
Console.WriteLine(user.LastPasswordSet);

如果LastPasswordSet的值为null,则"用户下次登录时必须更改密码"。