我有一个查询活动目录的方法,并将Last Password Reset的值返回给本地变量。我试图将该值与当前日期和时间进行比较,并检查它是否少于24小时。我认为我很接近,但似乎无法让它发挥作用。
谢谢, 杰森
string passwordLastSet = string.Empty;
passwordLastSet = DateTime.FromFileTime((Int64)(result.Properties["PwdLastSet"][0])).ToString();
public string lastReset(DateTime pwordLastReset)
{
if (DateTime.Now.AddHours(24) <= passwordLastSet)
{
return "try again later";
}
else
{
return "all is good";
}
}
答案 0 :(得分:7)
我试图将该值与当前日期和时间进行比较,并检查它是否少于24小时。
这段代码几乎写出来了。
DateTime timeOfLastPasswordReset = // get time of last password reset
DateTime now = DateTime.Now;
TimeSpan difference = now.Subtract(timeOfLastPasswordReset);
if(difference < TimeSpan.FromHours(24)) {
// password was reset less than twenty-four hours ago
}
else {
// password was reset no less than twenty-four hours ago
}
请注意代码的读取方式与您在英语中的指定完全相同。
答案 1 :(得分:6)
此:
if (DateTime.Now.AddHours(24) <= passwordLastSet)
应该是
if (DateTime.Now <= passwordLastSet.AddHours(24))
答案 2 :(得分:1)
怎么样(假设我已经正确地读了你的意图):
// Does passwordLastSet, with 24 hours added to it, fall before or after DateTime.Now?
// If AFTER, then reject, if BEFORE, then accept
if (passwordLastSet.Add(new TimeSpan(24, 0, 0)) > DateTime.Now)
{
// Password was last set within the last 24 hours
return "try again later";
}
else
{
return "all is good";
}
答案 3 :(得分:0)
尝试DateTime.Now.AddHours(-24) <= passwordLastSet
,因为您想模拟过去24小时,而不是将来
答案 4 :(得分:0)
您正在比较字符串变量和日期时间变量,无法比较它
DateTime passwordLastSet = DateTime.FromFileTime((Int64)(result.Properties["PwdLastSet"][0]));
public string lastReset(DateTime pwordLastReset)
{
if (DateTime.Now.AddHours(24) <= passwordLastSet)
{
return "try again later";
}
else
{
return "all is good";
}
}
将您的字符串更改为DateTime
如果您想将其与当前时间进行比较
只需使用此
if (DateTime.Now <= passwordLastSet)
{
return "try again later";
}
else
{
return "all is good";
}
如果要检查大于24,则更改为时间跨度并进行比较
答案 5 :(得分:0)
if (DateTime.Now.Subtract(passwordLastSet).TotalHours < 24)
Console.WriteLine("Try again");
else
Console.WriteLine("all is good");
您还可以使用TotalDays&lt; 1
答案 6 :(得分:0)
迂腐:
您需要在转换为DateTime之前检查特殊情况 - 例如pwdLastSet can be zero,因此您应该在尝试转换之前检查一下。
pwdLastSet
以UTC格式存储 - 因此,使用DateTime.FromFileTime
转换为本地时间可能会返回an ambiguous time。
因此,最好使用DateTime.FromFileTimeUtc
并与DateTime.UtcNow
进行比较。
根据您想要实现的目标,您可能还需要检查userAccountControl标志 - 类似于以下内容(未经测试):
[Flags]
private enum AdsUserFlags
{
Script = 0x1,
AccountDisabled = 0x2,
HomeDirectoryRequired = 0x8,
AccountLockedOut = 0x10,
PasswordNotRequired = 0x20,
PasswordCannotChange = 0x40,
EncryptedTextPasswordAllowed = 0x80,
TempDuplicateAccount = 0x100,
NormalAccount = 0x200,
InterDomainTrustAccount = 0x800,
WorkstationTrustAccount = 0x1000,
ServerTrustAccount = 0x2000,
PasswordDoesNotExpire = 0x10000,
MnsLogonAccount = 0x20000,
SmartCardRequired = 0x40000,
TrustedForDelegation = 0x80000,
AccountNotDelegated = 0x100000,
UseDesKeyOnly = 0x200000,
DontRequirePreauth = 0x400000,
PasswordExpired = 0x800000,
TrustedToAuthenticateForDelegation = 0x1000000,
NoAuthDataRequired = 0x2000000
}
...
AdsUserFlags userAccountControl = (AdsUserFlags)result.Properties["userAccountControl"][0];
long lastReset = (long)result.Properties["PwdLastSet"][0];
if (lastReset == 0L)
{
if ((userAccountControl & AdsUserFlags.PasswordDoesNotExpire) == 0)
{
// ... user must set password at next login
}
else
{
// ... presumably password has never been reset
}
}
else
{
DateTime lastResetUtc = DateTime.FromFileTimeUtc(lastReset);
// ... etc - compare with DateTime.UtcNow
}