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