2010-03-12 93 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纳秒为单位,所以你应该期望在数十亿值。

相关问题