Files
Aaru.Server/Aaru.Server.New/Components/Account/IdentityRevalidatingAuthenticationStateProvider.cs

41 lines
1.8 KiB
C#
Raw Normal View History

2024-05-02 07:43:47 +01:00
using System.Security.Claims;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
namespace Aaru.Server.New.Components.Account;
// This is a server-side AuthenticationStateProvider that revalidates the security stamp for the connected user
// every 30 minutes an interactive circuit is connected.
2024-05-03 03:24:40 +01:00
sealed class IdentityRevalidatingAuthenticationStateProvider
2024-05-02 07:43:47 +01:00
(ILoggerFactory loggerFactory, IServiceScopeFactory scopeFactory, IOptions<IdentityOptions> options)
: RevalidatingServerAuthenticationStateProvider(loggerFactory)
{
protected override TimeSpan RevalidationInterval => TimeSpan.FromMinutes(30);
protected override async Task<bool> ValidateAuthenticationStateAsync(
AuthenticationState authenticationState, CancellationToken cancellationToken)
{
// Get the user manager from a new scope to ensure it fetches fresh data
2024-05-03 03:24:40 +01:00
await using AsyncServiceScope scope = scopeFactory.CreateAsyncScope();
UserManager<IdentityUser> userManager = scope.ServiceProvider.GetRequiredService<UserManager<IdentityUser>>();
2024-05-02 07:43:47 +01:00
return await ValidateSecurityStampAsync(userManager, authenticationState.User);
}
async Task<bool> ValidateSecurityStampAsync(UserManager<IdentityUser> userManager, ClaimsPrincipal principal)
2024-05-02 07:43:47 +01:00
{
IdentityUser? user = await userManager.GetUserAsync(principal);
2024-05-03 03:24:40 +01:00
if(user is null) return false;
if(!userManager.SupportsUserSecurityStamp) return true;
string? principalStamp = principal.FindFirstValue(options.Value.ClaimsIdentity.SecurityStampClaimType);
string userStamp = await userManager.GetSecurityStampAsync(user);
return principalStamp == userStamp;
2024-05-02 07:43:47 +01:00
}
}