繁体   English   中英

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

[英]Detect if User Must Reset Password In Active Directory Using C#

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

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

DirectoryEntry user = ...

有没有我可以使用的财产:

user.Properties[someProperty];

条件存储在两个属性中:

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

这里开始

这是我写的这样做的。 不完全回答你的问题,但对后来阅读它的人有用。

重要的部分来自PrincipalContext on。 上面的所有内容就是我试图始终以正确的大小写返回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具有空值,则“用户必须在下次登录时更改密码”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM