Reputation: 9644
I've searched in stackoverflow how to find maxPwdAge for an AD user, and the solution should be something like this.
DirectoryEntry ldapConnection = new DirectoryEntry();
DirectorySearcher ldapSearch = new DirectorySearcher(ldapConnection);
ldapSearch.Filter = "samaccountname=" + stringWithUsername;
SearchResult user = ldapSearch.FindOne();
Now user
contains all user property, but i can't find maxPwdAge
string passowrdExpireTime = user.Properties["maxPwdAge"][0].ToString(); // It works
string passowrdExpireTime = user.Properties["pwdlastset"][0].ToString(); // pwdlastset doesn't exists
User's password does expire in 90 days, as I can find using this powershell command (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.TotalDays
.
Am I missing something?
Upvotes: 3
Views: 6078
Reputation: 11063
You need to use: Searcher.FindAll() ;
once you get the results from the DirectorySearcher
Check this code:
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();
}
Extracted from here:
Active Directory user password expiration date .NET/OU Group Policy
Upvotes: 5