I have a list of ADUsers and I want to check if any of the users' passwords are expiring soon, but I can't find any property to check.
This should do the trick to find out the time remaining until the password expires for each user:
private static TimeSpan GetTimeRemainingUntilPasswordExpiration(string domain, string userName)
{
using (var userEntry = new System.DirectoryServices.DirectoryEntry(string.Format("WinNT://{0}/{1},user", domain, userName)))
{
var maxPasswordAge = (int)userEntry.Properties.Cast<System.DirectoryServices.PropertyValueCollection>().First(p => p.PropertyName == "MaxPasswordAge").Value;
var passwordAge = (int)userEntry.Properties.Cast<System.DirectoryServices.PropertyValueCollection>().First(p => p.PropertyName == "PasswordAge").Value;
return TimeSpan.FromSeconds(maxPasswordAge) - TimeSpan.FromSeconds(passwordAge);
}
}
Note: you will need to add a reference to System.DirectoryServices
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With