@@ -23,13 +23,34 @@ public class SecurityStampValidator<TUser> : ISecurityStampValidator where TUser
2323 /// <param name="signInManager">The <see cref="SignInManager{TUser}"/>.</param>
2424 /// <param name="clock">The system clock.</param>
2525 /// <param name="logger">The logger.</param>
26+ [ Obsolete ( "ISystemClock is obsolete, use TimeProvider on SecurityStampValidatorOptions instead." ) ]
2627 public SecurityStampValidator ( IOptions < SecurityStampValidatorOptions > options , SignInManager < TUser > signInManager , ISystemClock clock , ILoggerFactory logger )
2728 {
2829 ArgumentNullException . ThrowIfNull ( options ) ;
2930 ArgumentNullException . ThrowIfNull ( signInManager ) ;
3031 SignInManager = signInManager ;
3132 Options = options . Value ;
32- Clock = clock ;
33+ TimeProvider = Options . TimeProvider ?? TimeProvider . System ;
34+ Clock = new TimeProviderClock ( TimeProvider ) ;
35+ Logger = logger . CreateLogger ( GetType ( ) ) ;
36+ }
37+
38+ /// <summary>
39+ /// Creates a new instance of <see cref="SecurityStampValidator{TUser}"/>.
40+ /// </summary>
41+ /// <param name="options">Used to access the <see cref="IdentityOptions"/>.</param>
42+ /// <param name="signInManager">The <see cref="SignInManager{TUser}"/>.</param>
43+ /// <param name="logger">The logger.</param>
44+ public SecurityStampValidator ( IOptions < SecurityStampValidatorOptions > options , SignInManager < TUser > signInManager , ILoggerFactory logger )
45+ {
46+ ArgumentNullException . ThrowIfNull ( options ) ;
47+ ArgumentNullException . ThrowIfNull ( signInManager ) ;
48+ SignInManager = signInManager ;
49+ Options = options . Value ;
50+ TimeProvider = Options . TimeProvider ?? TimeProvider . System ;
51+ #pragma warning disable CS0618 // Type or member is obsolete
52+ Clock = new TimeProviderClock ( TimeProvider ) ;
53+ #pragma warning restore CS0618 // Type or member is obsolete
3354 Logger = logger . CreateLogger ( GetType ( ) ) ;
3455 }
3556
@@ -46,8 +67,14 @@ public SecurityStampValidator(IOptions<SecurityStampValidatorOptions> options, S
4667 /// <summary>
4768 /// The <see cref="ISystemClock"/>.
4869 /// </summary>
70+ [ Obsolete ( "ISystemClock is obsolete, use TimeProvider instead." ) ]
4971 public ISystemClock Clock { get ; }
5072
73+ /// <summary>
74+ /// The <see cref="System.TimeProvider"/>.
75+ /// </summary>
76+ public TimeProvider TimeProvider { get ; }
77+
5178 /// <summary>
5279 /// Gets the <see cref="ILogger"/> used to log messages.
5380 /// </summary>
@@ -87,7 +114,7 @@ protected virtual async Task SecurityStampVerified(TUser user, CookieValidatePri
87114 {
88115 // On renewal calculate the new ticket length relative to now to avoid
89116 // extending the expiration.
90- context . Properties . IssuedUtc = Clock . UtcNow ;
117+ context . Properties . IssuedUtc = TimeProvider . GetUtcNow ( ) ;
91118 }
92119 }
93120
@@ -108,11 +135,7 @@ protected virtual async Task SecurityStampVerified(TUser user, CookieValidatePri
108135 /// <returns>The <see cref="Task"/> that represents the asynchronous validation operation.</returns>
109136 public virtual async Task ValidateAsync ( CookieValidatePrincipalContext context )
110137 {
111- var currentUtc = DateTimeOffset . UtcNow ;
112- if ( Clock != null )
113- {
114- currentUtc = Clock . UtcNow ;
115- }
138+ var currentUtc = TimeProvider . GetUtcNow ( ) ;
116139 var issuedUtc = context . Properties . IssuedUtc ;
117140
118141 // Only validate if enough time has elapsed
0 commit comments