@@ -23,13 +23,34 @@ public class SecurityStampValidator<TUser> : ISecurityStampValidator where TUser
23
23
/// <param name="signInManager">The <see cref="SignInManager{TUser}"/>.</param>
24
24
/// <param name="clock">The system clock.</param>
25
25
/// <param name="logger">The logger.</param>
26
+ [ Obsolete ( "ISystemClock is obsolete, use TimeProvider on SecurityStampValidatorOptions instead." ) ]
26
27
public SecurityStampValidator ( IOptions < SecurityStampValidatorOptions > options , SignInManager < TUser > signInManager , ISystemClock clock , ILoggerFactory logger )
27
28
{
28
29
ArgumentNullException . ThrowIfNull ( options ) ;
29
30
ArgumentNullException . ThrowIfNull ( signInManager ) ;
30
31
SignInManager = signInManager ;
31
32
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
33
54
Logger = logger . CreateLogger ( GetType ( ) ) ;
34
55
}
35
56
@@ -46,8 +67,14 @@ public SecurityStampValidator(IOptions<SecurityStampValidatorOptions> options, S
46
67
/// <summary>
47
68
/// The <see cref="ISystemClock"/>.
48
69
/// </summary>
70
+ [ Obsolete ( "ISystemClock is obsolete, use TimeProvider instead." ) ]
49
71
public ISystemClock Clock { get ; }
50
72
73
+ /// <summary>
74
+ /// The <see cref="System.TimeProvider"/>.
75
+ /// </summary>
76
+ public TimeProvider TimeProvider { get ; }
77
+
51
78
/// <summary>
52
79
/// Gets the <see cref="ILogger"/> used to log messages.
53
80
/// </summary>
@@ -87,7 +114,7 @@ protected virtual async Task SecurityStampVerified(TUser user, CookieValidatePri
87
114
{
88
115
// On renewal calculate the new ticket length relative to now to avoid
89
116
// extending the expiration.
90
- context . Properties . IssuedUtc = Clock . UtcNow ;
117
+ context . Properties . IssuedUtc = TimeProvider . GetUtcNow ( ) ;
91
118
}
92
119
}
93
120
@@ -108,11 +135,7 @@ protected virtual async Task SecurityStampVerified(TUser user, CookieValidatePri
108
135
/// <returns>The <see cref="Task"/> that represents the asynchronous validation operation.</returns>
109
136
public virtual async Task ValidateAsync ( CookieValidatePrincipalContext context )
110
137
{
111
- var currentUtc = DateTimeOffset . UtcNow ;
112
- if ( Clock != null )
113
- {
114
- currentUtc = Clock . UtcNow ;
115
- }
138
+ var currentUtc = TimeProvider . GetUtcNow ( ) ;
116
139
var issuedUtc = context . Properties . IssuedUtc ;
117
140
118
141
// Only validate if enough time has elapsed
0 commit comments