搜索到SO和其他地方,包括.net開發人員指南目錄服務編程手冊 - 沒有運氣。Windows 2008上的.NET Active Directory密碼過期
我想創建一個簡單的密碼重置網頁,允許用戶更改他們的密碼。代碼的更改密碼部分工作正常。對於我想在下一次當前密碼過期時顯示的用戶。
使用上面提到的書中的示例代碼,我能夠獲得所有的代碼設置,但返回的屬性總是等於Long.MinValue,因此不能倒置爲正數,此外,這意味着它沒有找到適當的域設置。
是否有人在Windows 2008或R2域環境中獲取密碼過期的示例代碼或引用,其中密碼策略對於每個用戶可能有所不同?
更新,包括代碼
構造一個獲取策略對象:
public PasswordExpires()
{
//Get Password Expiration
Domain domain = Domain.GetCurrentDomain();
DirectoryEntry root = domain.GetDirectoryEntry();
using (domain)
using (root)
{
this.policy = new DomainPolicy(root);
}
}
域策略構造:
public DomainPolicy(DirectoryEntry domainRoot)
{
string[] policyAttributes = new string[] {
"maxPwdAge", "minPwdAge", "minPwdLength",
"lockoutDuration", "lockOutObservationWindow",
"lockoutThreshold", "pwdProperties",
"pwdHistoryLength", "objectClass",
"distinguishedName"
};
//we take advantage of the marshaling with
//DirectorySearcher for LargeInteger values...
DirectorySearcher ds = new DirectorySearcher(
domainRoot,
"(objectClass=domainDNS)",
policyAttributes,
SearchScope.Base
);
SearchResult result = ds.FindOne();
//do some quick validation...
if (result == null)
{
throw new ArgumentException(
"domainRoot is not a domainDNS object."
);
}
this.attribs = result.Properties;
}
調用此方法來獲取密碼過期:
public TimeSpan MaxPasswordAge
{
get
{
string val = "maxPwdAge";
if (this.attribs.Contains(val))
{
long ticks = GetAbsValue(
this.attribs[val][0]
);
if (ticks > 0)
return TimeSpan.FromTicks(ticks);
}
return TimeSpan.MaxValue;
}
}
代碼在這裏失敗,因爲它不能轉換Long.MinValue,它不應該擺在首位
private long GetAbsValue(object longInt)
{
return Math.Abs((long)longInt);
}
這裏是調試器輸出和值。根據MSDN站點,溢出異常是由minvalue引起的。我的號碼與最小值的例子相符。
Screenshot http://www.brentpabst.com/capture.png
密碼過期是組策略的事情,不是嗎? – zneak 2010-03-12 02:53:13
@gabe更新它以包含代碼。你有什麼是有幫助的。 @zneak它通過組策略進行管理,但通過LDAP和其他機制公開 – 2010-03-12 03:13:18
您如何知道由於獲取'long.MinValue'而失敗? – Gabe 2010-03-12 03:15:36