2012-03-19 71 views

回答

10

这应该做的伎俩,找出剩余的时间,直到每个用户的密码过期:

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

+0

确保细粒度密码策略中的因素在使用它们或可能正在使用它们时使用。我不认为它确实如此,但是,我没有看到这里使用的WinNT提供程序。 – 2012-03-19 16:24:00

+1

如果域策略被组策略覆盖,这是一个正确的解决方案吗? – 2014-03-17 10:27:04