2012-03-19 94 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