Skip to content

Commit 4d4a9a3

Browse files
committed
Obsolete and replace ISystemClock in Security, Identity dotnet#47472
1 parent 0899b0a commit 4d4a9a3

File tree

52 files changed

+644
-180
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+644
-180
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
#nullable enable
2+
Microsoft.AspNetCore.Identity.SecurityStampValidator<TUser>.SecurityStampValidator(Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Identity.SecurityStampValidatorOptions!>! options, Microsoft.AspNetCore.Identity.SignInManager<TUser!>! signInManager, Microsoft.AspNetCore.Authentication.ISystemClock! clock, Microsoft.Extensions.Logging.ILoggerFactory! logger, System.TimeProvider! time) -> void
3+
Microsoft.AspNetCore.Identity.SecurityStampValidator<TUser>.SecurityStampValidator(Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Identity.SecurityStampValidatorOptions!>! options, Microsoft.AspNetCore.Identity.SignInManager<TUser!>! signInManager, System.TimeProvider! time, Microsoft.Extensions.Logging.ILoggerFactory! logger) -> void
4+
Microsoft.AspNetCore.Identity.SecurityStampValidator<TUser>.Time.get -> System.TimeProvider!
5+
Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator<TUser>.TwoFactorSecurityStampValidator(Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Identity.SecurityStampValidatorOptions!>! options, Microsoft.AspNetCore.Identity.SignInManager<TUser!>! signInManager, Microsoft.AspNetCore.Authentication.ISystemClock! clock, Microsoft.Extensions.Logging.ILoggerFactory! logger, System.TimeProvider! time) -> void
6+
Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator<TUser>.TwoFactorSecurityStampValidator(Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Identity.SecurityStampValidatorOptions!>! options, Microsoft.AspNetCore.Identity.SignInManager<TUser!>! signInManager, System.TimeProvider! time, Microsoft.Extensions.Logging.ILoggerFactory! logger) -> void
27
virtual Microsoft.AspNetCore.Identity.SignInManager<TUser>.IsTwoFactorEnabledAsync(TUser! user) -> System.Threading.Tasks.Task<bool>!

src/Identity/Core/src/SecurityStampValidator.cs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,52 @@ 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 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;
33+
Time = new TimeClockProvider(clock);
3234
Clock = clock;
3335
Logger = logger.CreateLogger(GetType());
3436
}
3537

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="clock">The system clock.</param>
44+
/// <param name="time">The system clock.</param>
45+
/// <param name="logger">The logger.</param>
46+
[Obsolete("ISystemClock is obsolete, use TimeProvider instead.")]
47+
public SecurityStampValidator(IOptions<SecurityStampValidatorOptions> options, SignInManager<TUser> signInManager, ISystemClock clock, ILoggerFactory logger, TimeProvider time)
48+
: this(options, signInManager, time, logger)
49+
{
50+
}
51+
52+
/// <summary>
53+
/// Creates a new instance of <see cref="SecurityStampValidator{TUser}"/>.
54+
/// </summary>
55+
/// <param name="options">Used to access the <see cref="IdentityOptions"/>.</param>
56+
/// <param name="signInManager">The <see cref="SignInManager{TUser}"/>.</param>
57+
/// <param name="time">The system clock.</param>
58+
/// <param name="logger">The logger.</param>
59+
public SecurityStampValidator(IOptions<SecurityStampValidatorOptions> options, SignInManager<TUser> signInManager, TimeProvider time, ILoggerFactory logger)
60+
{
61+
ArgumentNullException.ThrowIfNull(options);
62+
ArgumentNullException.ThrowIfNull(signInManager);
63+
SignInManager = signInManager;
64+
Options = options.Value;
65+
Time = time;
66+
#pragma warning disable CS0618 // Type or member is obsolete
67+
Clock = new SystemClock(time);
68+
#pragma warning restore CS0618 // Type or member is obsolete
69+
Logger = logger.CreateLogger(GetType());
70+
}
71+
3672
/// <summary>
3773
/// The SignInManager.
3874
/// </summary>
@@ -46,8 +82,14 @@ public SecurityStampValidator(IOptions<SecurityStampValidatorOptions> options, S
4682
/// <summary>
4783
/// The <see cref="ISystemClock"/>.
4884
/// </summary>
85+
[Obsolete("ISystemClock is obsolete, use TimeProvider instead.")]
4986
public ISystemClock Clock { get; }
5087

88+
/// <summary>
89+
/// The <see cref="ISystemClock"/>.
90+
/// </summary>
91+
public TimeProvider Time { get; }
92+
5193
/// <summary>
5294
/// Gets the <see cref="ILogger"/> used to log messages.
5395
/// </summary>
@@ -87,7 +129,7 @@ protected virtual async Task SecurityStampVerified(TUser user, CookieValidatePri
87129
{
88130
// On renewal calculate the new ticket length relative to now to avoid
89131
// extending the expiration.
90-
context.Properties.IssuedUtc = Clock.UtcNow;
132+
context.Properties.IssuedUtc = Time.GetUtcNow();
91133
}
92134
}
93135

@@ -109,9 +151,9 @@ protected virtual async Task SecurityStampVerified(TUser user, CookieValidatePri
109151
public virtual async Task ValidateAsync(CookieValidatePrincipalContext context)
110152
{
111153
var currentUtc = DateTimeOffset.UtcNow;
112-
if (Clock != null)
154+
if (Time != null)
113155
{
114-
currentUtc = Clock.UtcNow;
156+
currentUtc = Time.GetUtcNow();
115157
}
116158
var issuedUtc = context.Properties.IssuedUtc;
117159

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.AspNetCore.Authentication;
5+
6+
namespace Microsoft.AspNetCore.Identity;
7+
8+
internal sealed class TimeClockProvider : TimeProvider
9+
{
10+
#pragma warning disable CS0618 // Type or member is obsolete
11+
private readonly ISystemClock _clock;
12+
13+
public TimeClockProvider(ISystemClock clock)
14+
{
15+
_clock = clock;
16+
}
17+
#pragma warning restore CS0618 // Type or member is obsolete
18+
19+
public override DateTimeOffset GetUtcNow()
20+
{
21+
return _clock.UtcNow;
22+
}
23+
}

src/Identity/Core/src/TwoFactorSecurityStampValidator.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,32 @@ public class TwoFactorSecurityStampValidator<TUser> : SecurityStampValidator<TUs
2222
/// <param name="signInManager">The <see cref="SignInManager{TUser}"/>.</param>
2323
/// <param name="clock">The system clock.</param>
2424
/// <param name="logger">The logger.</param>
25+
[Obsolete("ISystemClock is obsolete, use TimeProvider instead.")]
2526
public TwoFactorSecurityStampValidator(IOptions<SecurityStampValidatorOptions> options, SignInManager<TUser> signInManager, ISystemClock clock, ILoggerFactory logger) : base(options, signInManager, clock, logger)
2627
{ }
2728

29+
/// <summary>
30+
/// Creates a new instance of <see cref="SecurityStampValidator{TUser}"/>.
31+
/// </summary>
32+
/// <param name="options">Used to access the <see cref="IdentityOptions"/>.</param>
33+
/// <param name="signInManager">The <see cref="SignInManager{TUser}"/>.</param>
34+
/// <param name="clock">The system clock.</param>
35+
/// <param name="time">The system clock.</param>
36+
/// <param name="logger">The logger.</param>
37+
[Obsolete("ISystemClock is obsolete, use TimeProvider instead.")]
38+
public TwoFactorSecurityStampValidator(IOptions<SecurityStampValidatorOptions> options, SignInManager<TUser> signInManager, ISystemClock clock, ILoggerFactory logger, TimeProvider time) : base(options, signInManager, time, logger)
39+
{ }
40+
41+
/// <summary>
42+
/// Creates a new instance of <see cref="SecurityStampValidator{TUser}"/>.
43+
/// </summary>
44+
/// <param name="options">Used to access the <see cref="IdentityOptions"/>.</param>
45+
/// <param name="signInManager">The <see cref="SignInManager{TUser}"/>.</param>
46+
/// <param name="time">The system clock.</param>
47+
/// <param name="logger">The logger.</param>
48+
public TwoFactorSecurityStampValidator(IOptions<SecurityStampValidatorOptions> options, SignInManager<TUser> signInManager, TimeProvider time, ILoggerFactory logger) : base(options, signInManager, time, logger)
49+
{ }
50+
2851
/// <summary>
2952
/// Verifies the principal's security stamp, returns the matching user if successful
3053
/// </summary>

0 commit comments

Comments
 (0)