5

I have a method that queries active directory, and returns the value of the Last Password Reset to a local variable. I am trying to compare that value to the current date and time, and check if it's been less than 24 hours. I think I'm close, but can't seem to get this to work.

Thanks, Jason

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";
    }
}
Vladislav Povorozniuc
  • 2,149
  • 25
  • 26
Jason
  • 333
  • 1
  • 6
  • 17

7 Answers7

7

I am trying to compare that value to the current date and time, and check if it's been less than 24 hours.

This code almost writes itself.

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
}

Note how the code reads exactly as you specified in English.

jason
  • 236,483
  • 35
  • 423
  • 525
6

This:

 if (DateTime.Now.AddHours(24) <= passwordLastSet)

should be

   if (DateTime.Now <= passwordLastSet.AddHours(24))
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
1

How about (assuming I've read your intentions correctly):

// 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";
}
Rob
  • 45,296
  • 24
  • 122
  • 150
0

try DateTime.Now.AddHours(-24) <= passwordLastSet as you want to simulate that now is 24 hours in past, not in future

Milen
  • 8,697
  • 7
  • 43
  • 57
Naveed Ahmad
  • 445
  • 3
  • 6
0

You are comparing a string variable and a datetime variable, it is not possible to compare them

 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";
     }
 }

Change your string to DateTime if you want to compare it with current time.

Just use this

if (DateTime.Now <= passwordLastSet)
{
    return "try again later";
}
else
{
    return "all is good";
}

If you want to check it greater than 24 change both to TimeSpan and compare it.

Vladislav Povorozniuc
  • 2,149
  • 25
  • 26
Nighil
  • 4,099
  • 7
  • 30
  • 56
  • if i change String passwordLastSet to DateTime password last set, I get a compile error "Cannot implicitly convert type 'string' to 'System.DateTime' which I'm reading as I need to change the return type, or cast string to date time. – Jason Feb 10 '11 at 18:52
  • DateTime.FromFileTime((Int64)(result.Properties["PwdLastSet"][0])).ToString(); this line is causing error – Nighil Feb 10 '11 at 19:07
  • change it DateTime.FromFileTime((Int64)(result.Properties["PwdLastSet"][0])); – Nighil Feb 10 '11 at 19:08
  • I just realized the answer to my own comment, I just need to remove .ToString – Jason Feb 10 '11 at 19:10
0
if (DateTime.Now.Subtract(passwordLastSet).TotalHours < 24)
    Console.WriteLine("Try again");
else
    Console.WriteLine("all is good");

You can also use TotalDays < 1

Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
0

To be pedantic:

  • You need to check for special cases before converting to DateTime - for example pwdLastSet can be zero, so you should check this before attempting to convert.

  • pwdLastSet is stored as UTC - so that converting to local time using DateTime.FromFileTime might return an ambiguous time.

    So it would be better to use DateTime.FromFileTimeUtc and compare with DateTime.UtcNow.

Depending on exactly what you want to achieve, you may also want to check the userAccountControl flags - something like the following (untested):

    [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
    }
Community
  • 1
  • 1
Joe
  • 122,218
  • 32
  • 205
  • 338