gpt4 book ai didi

c# - Windows 2008 上的 .NET Active Directory 密码过期

转载 作者:行者123 更新时间:2023-11-30 17:23:03 24 4
gpt4 key购买 nike

搜索了 SO 和其他任何地方,包括 .net 开发人员指南目录服务编程书籍 - 没有运气。

我正在尝试创建一个简单的密码重置网页,允许用户更改密码。代码的更改密码部分工作正常。对于用户,我还想显示他们当前密码下一次到期的时间。

使用上面提到的书中的示例代码,我能够获得所有代码设置,但是,返回的属性始终等于 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 站点,溢出异常是由最小值引起的。我的数字与最小值的示例匹配。

Screenshot http://www.brentpabst.com/capture.png

最佳答案

密码过期时间的存储使得如果lastPwdSet - maxPwdAge < DateTime.UtcNow为真,则您的密码已过期。因此,如果您一周前设置了密码,但密码将在 10 天后过期,则左侧将为 (DateTime.UtcNow - 7) - (-10) , 或 DateTime.UtcNow - 7 + 10 , 或 DateTime.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 纳秒的增量存储,因此您应该期望值以数十亿为单位。

关于c# - Windows 2008 上的 .NET Active Directory 密码过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2430210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com