2010-03-12 86 views
3

搜索到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

+0

密碼過期是組策略的事情,不是嗎? – zneak 2010-03-12 02:53:13

+0

@gabe更新它以包含代碼。你有什麼是有幫助的。 @zneak它通過組策略進行管理,但通過LDAP和其他機制公開 – 2010-03-12 03:13:18

+0

您如何知道由於獲取'long.MinValue'而失敗? – Gabe 2010-03-12 03:15:36

回答

2

密碼過期時間被存儲,如果lastPwdSet - maxPwdAge < DateTime.UtcNow是真的,那麼您的密碼已過期。因此,如果您在一週前設置了密碼,但密碼將在10天后過期,則左側將爲(DateTime.UtcNow - 7) - (-10)DateTime.UtcNow - 7 + 10DateTime.UtcNow + 3,這不會低於DateTime.UtcNow,因此您的密碼不會過期。

這意味着將maxPwdAge設置爲long.MinValue將在密碼過期前爲您提供數千年的有效幫助。所以如果你得到long.MinValue,你的政策說密碼不會過期。你應該尋找那些價值和正確地對待它,可能是這樣的:

private long GetAbsValue(object longInt) // poorly named 
{ 
    long val = (long)longInt; 
    if (val == long.MinValue) 
     return long.MaxValue; 
    return Math.Abs((long)longInt); 
} 

另外,我要指出的是,值存儲在100納秒爲單位,所以你應該期望在數十億值。

相關問題