Skip to content
Open
Changes from 1 commit
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
22 changes: 20 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,12 @@ public virtual async Task<bool> IsLockedOutAsync(TUser user)
return false;
}
var lockoutTime = await store.GetLockoutEndDateAsync(user, CancellationToken).ConfigureAwait(false);
return lockoutTime >= DateTimeOffset.UtcNow;
#if NET8_0_OR_GREATER
var utcNow = UtcNow();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you made the #if just change the behaviour of UtcNow() you could do it in just one place and then use the method everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using #if inside UtcNow() causes CA1822 error and results in build failure. This happens because the ServiceProvider field is used only in .NET 8 and higher versions. For older versions, this method should be marked static.

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

Another solution I see is to create two separate methods for UtcNow(). One of them should be static for older versions and the other should not.

#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

If you have any other ideas or approaches for this, please let me know.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it were me I would do the second one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed UtcNow method implementation

#else
var utcNow = DateTimeOffset.UtcNow;
#endif
return lockoutTime >= utcNow;
}

/// <summary>
Expand Down Expand Up @@ -2186,7 +2191,12 @@ 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),
#if NET8_0_OR_GREATER
var utcNow = UtcNow();
#else
var utcNow = DateTimeOffset.UtcNow;
#endif
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 +2689,14 @@ protected virtual void Dispose(bool disposing)
}
}

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

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