Skip to content

Added usage of TimeProvider instead of DateTimeOffset #63042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/Identity/Extensions.Core/src/UserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2067,7 +2067,7 @@ public virtual async Task<bool> IsLockedOutAsync(TUser user)
return false;
}
var lockoutTime = await store.GetLockoutEndDateAsync(user, CancellationToken).ConfigureAwait(false);
return lockoutTime >= DateTimeOffset.UtcNow;
return lockoutTime >= UtcNow();
}

/// <summary>
Expand Down Expand Up @@ -2186,7 +2186,7 @@ public virtual async Task<IdentityResult> AccessFailedAsync(TUser user)
return await UpdateUserAndRecordMetricAsync(user, UserUpdateType.AccessFailed).ConfigureAwait(false);
}
Logger.LogDebug(LoggerEventIds.UserLockedOut, "User is locked out.");
await store.SetLockoutEndDateAsync(user, DateTimeOffset.UtcNow.Add(Options.Lockout.DefaultLockoutTimeSpan),
await store.SetLockoutEndDateAsync(user, UtcNow().Add(Options.Lockout.DefaultLockoutTimeSpan),
CancellationToken).ConfigureAwait(false);
await store.ResetAccessFailedCountAsync(user, CancellationToken).ConfigureAwait(false);
return await UpdateUserAndRecordMetricAsync(user, UserUpdateType.AccessFailed).ConfigureAwait(false);
Expand Down Expand Up @@ -2679,6 +2679,19 @@ protected virtual void Dispose(bool disposing)
}
}

#if NET8_0_OR_GREATER
private DateTimeOffset UtcNow()
{
var timeProvider = ServiceProvider.GetService<TimeProvider>();
return timeProvider?.GetUtcNow() ?? DateTimeOffset.UtcNow;
}
#else
private static DateTimeOffset UtcNow()
{
return DateTimeOffset.UtcNow;
}
#endif

private IUserTwoFactorStore<TUser> GetUserTwoFactorStore()
{
var cast = Store as IUserTwoFactorStore<TUser>;
Expand Down
Loading