Skip to content

Conversation

StickFun
Copy link
Contributor

@StickFun StickFun commented Aug 1, 2025

Added usage of TimeProvider instead of DateTimeOffset

Description

Added usage of TimeProvider for NET 8 and higher versions.
Other versions using DateTimeOffset

Fixes #62796

@github-actions github-actions bot added the area-identity Includes: Identity and providers label Aug 1, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Aug 1, 2025
@StickFun
Copy link
Contributor Author

StickFun commented Aug 1, 2025

@dotnet-policy-service agree

@@ -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

@StickFun StickFun requested a review from martincostello August 6, 2025 18:09
Copy link
Member

@martincostello martincostello left a comment

Choose a reason for hiding this comment

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

Maybe add a test that demonstrates it's used for .NET 8.0+, but otherwise looks ok to me.

@StickFun
Copy link
Contributor Author

StickFun commented Aug 7, 2025

Maybe add a test that demonstrates it's used for .NET 8.0+, but otherwise looks ok to me.

Added unit test that verifies using TimeProvider

@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Aug 15, 2025
@StickFun
Copy link
Contributor Author

StickFun commented Aug 18, 2025

@martincostello, do I need to rerun CI/CD myself and ping reviewer for last approve?

@martincostello
Copy link
Member

If you close and reopen the PR it will run it again.

@StickFun StickFun closed this Aug 20, 2025
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-rc1 milestone Aug 20, 2025
@StickFun StickFun reopened this Aug 20, 2025
@StickFun
Copy link
Contributor Author

@martincostello, is there anything else I should do to merge this PR?

@martincostello
Copy link
Member

Assuming the test failures aren't due to your changes, you'll just have to wait for someone from the aspnetcore team to look at it and merge it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-identity Includes: Identity and providers community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use TimeProvider instead of DateTimeOffset in UserManager
3 participants