C#can';在用户中找不到maxPwdAge属性

C#can';在用户中找不到maxPwdAge属性,c#,active-directory,C#,Active Directory,我在stackoverflow中搜索了如何为广告用户查找maxPwdAge,解决方案应该是这样的 DirectoryEntry ldapConnection = new DirectoryEntry(); DirectorySearcher ldapSearch = new DirectorySearcher(ldapConnection); ldapSearch.Filter = "samaccountname=" + stringWithUsername; SearchResult use

我在stackoverflow中搜索了如何为广告用户查找maxPwdAge,解决方案应该是这样的

DirectoryEntry ldapConnection = new DirectoryEntry();
DirectorySearcher ldapSearch = new DirectorySearcher(ldapConnection);

ldapSearch.Filter = "samaccountname=" + stringWithUsername;
SearchResult user = ldapSearch.FindOne();
现在
user
包含所有用户属性,但我找不到maxPwdAge

string passowrdExpireTime = user.Properties["maxPwdAge"][0].ToString(); // It works
string passowrdExpireTime = user.Properties["pwdlastset"][0].ToString(); // pwdlastset doesn't exists
用户密码将在90天后过期,我可以使用此powershell命令
(Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.TotalDays

我遗漏了什么吗?

您需要使用:
Searcher.FindAll()
一旦您从DirectorySearcher获得结果

检查此代码:

            string domainAndUsername = string.Empty;
            string domain = string.Empty;
            string userName = string.Empty;
            string passWord = string.Empty;
            AuthenticationTypes at = AuthenticationTypes.Anonymous;
            StringBuilder sb = new StringBuilder();

            domain = @"LDAP://w.x.y.z";
            domainAndUsername = @"LDAP://w.x.y.z/cn=Lawrence E."+
                        " Smithmier\, Jr.,cn=Users,dc=corp,"+
                        "dc=productiveedge,dc=com";
            userName = "Administrator";
            passWord = "xxxpasswordxxx";
            at = AuthenticationTypes.Secure;

            DirectoryEntry entry = new DirectoryEntry(
                        domain, userName, passWord, at);

            DirectorySearcher mySearcher = new DirectorySearcher(entry);

            SearchResultCollection results;
            string filter = "maxPwdAge=*";
            mySearcher.Filter = filter;

            results = mySearcher.FindAll();
            long maxDays = 0;
            if(results.Count>=1)
            {
                Int64 maxPwdAge=(Int64)results[0].Properties["maxPwdAge"][0];
                maxDays = maxPwdAge/-864000000000;
            }

            DirectoryEntry entryUser = new DirectoryEntry(
                        domainAndUsername, userName, passWord, at);
            mySearcher = new DirectorySearcher(entryUser);

            results = mySearcher.FindAll();
            long daysLeft=0;
            if (results.Count >= 1)
            {
                var lastChanged = results[0].Properties["pwdLastSet"][0];
                daysLeft = maxDays - DateTime.Today.Subtract(
                        DateTime.FromFileTime((long)lastChanged)).Days;
            }
            Console.WriteLine(
                        String.Format("You must change your password within"+
                                      " {0} days"
                                     , daysLeft));
            Console.ReadLine();
        }
摘自:


您需要使用:
Searcher.FindAll()
一旦您从DirectorySearcher获得结果

检查此代码:

            string domainAndUsername = string.Empty;
            string domain = string.Empty;
            string userName = string.Empty;
            string passWord = string.Empty;
            AuthenticationTypes at = AuthenticationTypes.Anonymous;
            StringBuilder sb = new StringBuilder();

            domain = @"LDAP://w.x.y.z";
            domainAndUsername = @"LDAP://w.x.y.z/cn=Lawrence E."+
                        " Smithmier\, Jr.,cn=Users,dc=corp,"+
                        "dc=productiveedge,dc=com";
            userName = "Administrator";
            passWord = "xxxpasswordxxx";
            at = AuthenticationTypes.Secure;

            DirectoryEntry entry = new DirectoryEntry(
                        domain, userName, passWord, at);

            DirectorySearcher mySearcher = new DirectorySearcher(entry);

            SearchResultCollection results;
            string filter = "maxPwdAge=*";
            mySearcher.Filter = filter;

            results = mySearcher.FindAll();
            long maxDays = 0;
            if(results.Count>=1)
            {
                Int64 maxPwdAge=(Int64)results[0].Properties["maxPwdAge"][0];
                maxDays = maxPwdAge/-864000000000;
            }

            DirectoryEntry entryUser = new DirectoryEntry(
                        domainAndUsername, userName, passWord, at);
            mySearcher = new DirectorySearcher(entryUser);

            results = mySearcher.FindAll();
            long daysLeft=0;
            if (results.Count >= 1)
            {
                var lastChanged = results[0].Properties["pwdLastSet"][0];
                daysLeft = maxDays - DateTime.Today.Subtract(
                        DateTime.FromFileTime((long)lastChanged)).Days;
            }
            Console.WriteLine(
                        String.Format("You must change your password within"+
                                      " {0} days"
                                     , daysLeft));
            Console.ReadLine();
        }
摘自:


您是否见过此示例?您是否见过使用
System.DirectoryServices.AccountManagement
的任何代码示例?是否见过使用
System.DirectoryServices.AccountManagement
的任何代码示例?