c# – 如何找到ADUser的密码到期日期或密码到期之前的剩余天数?

我有一个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的引用.

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么