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