活动目录 - 获取密码即将到期的用户列表

问题描述 投票:-1回答:1

我需要从Active目录中获取一个用户列表,其密码即将到期(例如在5天内)。

我需要通过向DirectorySearcher添加过滤器来实现这一点,因为它将是最快的。我已将samaccountname模式添加到过滤器,但我无法弄清楚如何添加pwdLastSet。理想情况下,过滤器会将用户列表减少到仅满足密码过期标准的用户列表。

        using (DirectoryEntry searchRoot = GetXYZAccountOU())
        {
            DirectorySearcher ds = new DirectorySearcher(searchRoot);
            ds.SearchScope = SearchScope.Subtree;

            ds.Filter = "(&" +                                
                            "(samaccountname=XYZ*)"
             + ")";


            SearchResultCollection result = ds.FindAll();

            foreach (SearchResult searchResult in result)
            {
                var de = searchResult.GetDirectoryEntry();
                //long pwdLastSetVal = (long)de.Properties["pwdLastSet"][0];

                //Console.WriteLine(de.Properties["displayName"].Value + ": " + DateTime.FromFileTimeUtc(pwdLastSetVal));
                Console.WriteLine(de.Properties["displayName"].Value);
            }

            Console.Read();
        }

这里XYZ是我的用户samaccountname的起始字母。

如果我运行此代码,我可以获取displayName和其他一些属性,但不能获得pwdLastSet或计算属性msDS-UserPasswordExpiryTimeComputed,而我可以在Active Directory浏览器中看到它们。

c# active-directory directorysearcher
1个回答
0
投票

您必须事先知道密码的有效期,并查询pwdLastSet属性。但是,当然日期以奇怪的格式存储。

我们假设它们有效期为30天。然后你可以像这样构造查询:

var date = DateTime.Now.AddDays(-30).ToFileTime();
var query = $"(&(objectclass=user)(objectcategory=person)(!pwdlastset=0)(pwdlastset<={date})(!userAccountControl:1.2.840.113556.1.4.803:=65536))";

可以将帐户设置为永不过期密码,因此您必须在查询中考虑该帐户。 userAccountControl条件就是这样。

如果您不想在代码中使用幻数,可以通过查看域根目录下的maxPwdAge属性(以不同的,奇怪的格式存储)来查找密码在域上有效的时间长度):

var domain = new DirectoryEntry("LDAP://domain.com");
Int64 pwdAge = (Int64) domain.Properties["maxPwdAge"][0];
var maxPwdAge = pwdAge / -864000000000; //convert to days
© www.soinside.com 2019 - 2024. All rights reserved.