检查Active Directory过期日期

时间:2016-09-13 19:47:12

标签: c# active-directory userprincipal

我有一个程序对活动目录中的对象运行一些验证,我的一个检查是查看是否在一年内设置了到期日期。使用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之类的东西是由我制作的自定义对象,但我希望这不会阻止任何人回答我的问题;

  

我如何检查有效期是否设定为一年或更短

2 个答案:

答案 0 :(得分:0)

您应该能够检查与当前日期的差异,并查看过期天数是否小于365或您想要的任何参考日期。但是你可以从差异中得到所有类型的值并与之比较

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

答案 1 :(得分:0)

很难确切地说出你所追求的是什么,但这可能会有所帮助。

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