Time check

I have a method that queries the active directory and returns the value of the last Reset password of a local variable. I am trying to compare this value with the current date and time and check if it was less than 24 hours. I think I'm near, but I can't get it 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"; } } 
+4
source share
7 answers

It:

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

it should be

  if (DateTime.Now <= passwordLastSet.AddHours(24)) 
+6
source

I am trying to compare this value with the current date and time and check if it was less than 24 hours.

This code is almost written by 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 } 

Notice how the code reads exactly as you indicated in English.

+6
source

How about (if I 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"; } 
+1
source

try DateTime.Now.AddHours(-24) <= passwordLastSet , how do you want to simulate that 24 hours have passed now, and not in the future

0
source

you are comparing a string variable and a datetime variable that cannot be compared with it.

  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 the current time

just use it

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

if you want to check it over 24, change both values ​​for a while and compare it

0
source
 if (DateTime.Now.Subtract(passwordLastSet).TotalHours < 24) Console.WriteLine("Try again"); else Console.WriteLine("all is good"); 

You can also use TotalDays <1

0
source

To be pedantic:

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

  • pwdLastSet is stored as UTC - so converting to local time using DateTime.FromFileTime can return an undefined time .

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

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

  [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 } 
0
source

Source: https://habr.com/ru/post/1339250/


All Articles