[英]How can I find out an ADUser's password expiry date or days left until password expiry?
我有一个ADUsers列表,我想检查是否有任何用户的密码即将到期,但我找不到任何要检查的属性。
这应该可以找到每个用户密码到期之前的剩余时间:
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);
}
}
注意:您需要添加对System.DirectoryServices
的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.