Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查时间跨度_C#_.net_.net 3.5 - Fatal编程技术网

C# 检查时间跨度

C# 检查时间跨度,c#,.net,.net-3.5,C#,.net,.net 3.5,我有一个方法可以查询active directory,并将上次密码重置的值返回到局部变量。我试图将该值与当前日期和时间进行比较,并检查是否已少于24小时。我想我已经很接近了,但似乎无法让它发挥作用 谢谢, 杰森 string passwordLastSet=string.Empty; passwordLastSet=DateTime.FromFileTime((Int64)(result.Properties[“PwdLastSet”][0])).ToString(); 公共字符串lastRes

我有一个方法可以查询active directory,并将上次密码重置的值返回到局部变量。我试图将该值与当前日期和时间进行比较,并检查是否已少于24小时。我想我已经很接近了,但似乎无法让它发挥作用

谢谢, 杰森

string passwordLastSet=string.Empty;
passwordLastSet=DateTime.FromFileTime((Int64)(result.Properties[“PwdLastSet”][0])).ToString();
公共字符串lastReset(日期时间pwordLastReset)
{
如果(DateTime.Now.AddHours(24)怎么样(假设我正确阅读了您的意图):


尝试
DateTime.Now.AddHours(-24)

if(DateTime.Now.AddHours(24)
我试图将该值与当前日期和时间进行比较,并检查是否已少于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
}
DateTime timeOfLastPasswordReset=//获取上次密码重置的时间
DateTime now=DateTime.now;
TimeSpan差=now.Subtract(LastPasswordReset的时间);
如果(差值<时间跨度,从小时数(24)){
//密码在24小时前被重置
}
否则{
//密码在24小时前被重置
}

请注意代码的读取方式与您在英语中指定的完全一致。

如果要比较字符串变量和日期时间变量,则无法进行比较

 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 passwordLastSet=DateTime.FromFileTime((Int64)(result.Properties[“PwdLastSet”][0]);
公共字符串lastReset(日期时间pwordLastReset)
{
if(DateTime.Now.AddHours(24)
if(DateTime.Now.Subtract(passwordLastSet).TotalHours<24)
Console.WriteLine(“重试”);
其他的
Console.WriteLine(“一切都很好”);
你也可以用TotalDays<1

来装腔作势:

  • 您需要在转换为DateTime之前检查特殊情况-例如,因此您应该在尝试转换之前检查此情况

  • pwdLastSet
    存储为UTC,因此使用
    DateTime.FromFileTime
    转换为本地时间可能会返回

    因此,最好使用
    DateTime.FromFileTimeUtc
    并与
    DateTime.UtcNow
    进行比较

根据您想要实现的目标,您可能还需要检查userAccountControl标志-类似于以下内容(未测试):


如果我将字符串passwordLastSet更改为DateTime password LastSet,则会出现编译错误“无法将类型“String”隐式转换为我正在读取的“System.DateTime”,因为我需要更改返回类型,或者将字符串转换为date time.DateTime.FromFileTime((Int64)(result.Properties[“PwdLastSet”][0])。ToString();此行导致错误更改为DateTime.FromFileTime((Int64)(result.Properties[“PwdLastSet”][0]);我刚刚意识到我自己的评论的答案,我只需要删除.ToString
 if (DateTime.Now.AddHours(24) <= passwordLastSet)
   if (DateTime.Now <= passwordLastSet.AddHours(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
}
 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";
     }
 }
if (DateTime.Now <= passwordLastSet)
{
    return "try again later";
}
else
{
    return "all is good";
}
if (DateTime.Now.Subtract(passwordLastSet).TotalHours < 24)
    Console.WriteLine("Try again");
else
    Console.WriteLine("all is good");
    [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
    }