检查 Active Directory 过期日期

Checking Active Directory Expire Dates

我有一个程序可以对活动目录中的对象运行一些验证,我的一项检查是查看到期日期是否设置在一年内。使用 UserPrincipal 对象,我可以检查 .AccountExpirationDate 日期以查看它是否有日期,但我如何查看该日期以查看它是否设置为在一年内到期?

这就是我目前正在使用的

protected Check AccountExpiresMandatoryCheck = new Check()
{
    ResultMessageTemplate = "User(s) don't have an expiry date or expiry date is greater than 1 year",
    Run = delegate(Principal principal, AccountPoint point)
    {
        UserPrincipal user = principal as UserPrincipal;
        if (user == null) return false;
        return user.AccountExpirationDate != null || //check here if the date is a year or less;
    }
};

我知道 CheckAccountPoint 之类的东西是我制作的自定义对象,但我希望这不会阻止任何人回答我的问题;

How would I check if the expiry date was set to be a year or less

您应该能够检查与当前日期的差异,并查看到期天数是否少于 365 天或是否与您想要的任何参考日期相对应。但是你可以从差异中得到各种值并与它进行比较

var dateDifference = theUser.AccountExpirationDate - DateTime.Now;
if (dateDifference != null)
    Debug.WriteLine(dateDifference.Value.Days);

很难准确地说出您在寻找什么,但这可能会有所帮助。

DateTime oneYearAgoToday = DateTime.Today.AddYears(-1);
return user.AccountExpirationDate != null || user.AccountExpirationDate > oneYearAgoToday;