|
5 | 5 | using Microsoft.AspNetCore.Identity;
|
6 | 6 | using Microsoft.Extensions.Options;
|
7 | 7 |
|
8 |
| -namespace BlazorServerWeb_CSharp.Areas.Identity |
| 8 | +namespace BlazorServerWeb_CSharp.Areas.Identity; |
| 9 | + |
| 10 | +public class RevalidatingIdentityAuthenticationStateProvider<TUser> |
| 11 | + : RevalidatingServerAuthenticationStateProvider where TUser : class |
9 | 12 | {
|
10 |
| - public class RevalidatingIdentityAuthenticationStateProvider<TUser> |
11 |
| - : RevalidatingServerAuthenticationStateProvider where TUser : class |
| 13 | + private readonly IServiceScopeFactory _scopeFactory; |
| 14 | + private readonly IdentityOptions _options; |
| 15 | + |
| 16 | + public RevalidatingIdentityAuthenticationStateProvider( |
| 17 | + ILoggerFactory loggerFactory, |
| 18 | + IServiceScopeFactory scopeFactory, |
| 19 | + IOptions<IdentityOptions> optionsAccessor) |
| 20 | + : base(loggerFactory) |
12 | 21 | {
|
13 |
| - private readonly IServiceScopeFactory _scopeFactory; |
14 |
| - private readonly IdentityOptions _options; |
| 22 | + _scopeFactory = scopeFactory; |
| 23 | + _options = optionsAccessor.Value; |
| 24 | + } |
15 | 25 |
|
16 |
| - public RevalidatingIdentityAuthenticationStateProvider( |
17 |
| - ILoggerFactory loggerFactory, |
18 |
| - IServiceScopeFactory scopeFactory, |
19 |
| - IOptions<IdentityOptions> optionsAccessor) |
20 |
| - : base(loggerFactory) |
| 26 | + protected override TimeSpan RevalidationInterval => TimeSpan.FromMinutes(30); |
| 27 | + |
| 28 | + protected override async Task<bool> ValidateAuthenticationStateAsync( |
| 29 | + AuthenticationState authenticationState, CancellationToken cancellationToken) |
| 30 | + { |
| 31 | + // Get the user manager from a new scope to ensure it fetches fresh data |
| 32 | + var scope = _scopeFactory.CreateScope(); |
| 33 | + try |
21 | 34 | {
|
22 |
| - _scopeFactory = scopeFactory; |
23 |
| - _options = optionsAccessor.Value; |
| 35 | + var userManager = scope.ServiceProvider.GetRequiredService<UserManager<TUser>>(); |
| 36 | + return await ValidateSecurityStampAsync(userManager, authenticationState.User); |
24 | 37 | }
|
25 |
| - |
26 |
| - protected override TimeSpan RevalidationInterval => TimeSpan.FromMinutes(30); |
27 |
| - |
28 |
| - protected override async Task<bool> ValidateAuthenticationStateAsync( |
29 |
| - AuthenticationState authenticationState, CancellationToken cancellationToken) |
| 38 | + finally |
30 | 39 | {
|
31 |
| - // Get the user manager from a new scope to ensure it fetches fresh data |
32 |
| - var scope = _scopeFactory.CreateScope(); |
33 |
| - try |
| 40 | + if (scope is IAsyncDisposable asyncDisposable) |
34 | 41 | {
|
35 |
| - var userManager = scope.ServiceProvider.GetRequiredService<UserManager<TUser>>(); |
36 |
| - return await ValidateSecurityStampAsync(userManager, authenticationState.User); |
| 42 | + await asyncDisposable.DisposeAsync(); |
37 | 43 | }
|
38 |
| - finally |
| 44 | + else |
39 | 45 | {
|
40 |
| - if (scope is IAsyncDisposable asyncDisposable) |
41 |
| - { |
42 |
| - await asyncDisposable.DisposeAsync(); |
43 |
| - } |
44 |
| - else |
45 |
| - { |
46 |
| - scope.Dispose(); |
47 |
| - } |
| 46 | + scope.Dispose(); |
48 | 47 | }
|
49 | 48 | }
|
| 49 | + } |
50 | 50 |
|
51 |
| - private async Task<bool> ValidateSecurityStampAsync(UserManager<TUser> userManager, ClaimsPrincipal principal) |
| 51 | + private async Task<bool> ValidateSecurityStampAsync(UserManager<TUser> userManager, ClaimsPrincipal principal) |
| 52 | + { |
| 53 | + var user = await userManager.GetUserAsync(principal); |
| 54 | + if (user == null) |
52 | 55 | {
|
53 |
| - var user = await userManager.GetUserAsync(principal); |
54 |
| - if (user == null) |
55 |
| - { |
56 |
| - return false; |
57 |
| - } |
58 |
| - else if (!userManager.SupportsUserSecurityStamp) |
59 |
| - { |
60 |
| - return true; |
61 |
| - } |
62 |
| - else |
63 |
| - { |
64 |
| - var principalStamp = principal.FindFirstValue(_options.ClaimsIdentity.SecurityStampClaimType); |
65 |
| - var userStamp = await userManager.GetSecurityStampAsync(user); |
66 |
| - return principalStamp == userStamp; |
67 |
| - } |
| 56 | + return false; |
| 57 | + } |
| 58 | + else if (!userManager.SupportsUserSecurityStamp) |
| 59 | + { |
| 60 | + return true; |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + var principalStamp = principal.FindFirstValue(_options.ClaimsIdentity.SecurityStampClaimType); |
| 65 | + var userStamp = await userManager.GetSecurityStampAsync(user); |
| 66 | + return principalStamp == userStamp; |
68 | 67 | }
|
69 | 68 | }
|
70 | 69 | }
|
0 commit comments