mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 19:24:27 +00:00
Reformat new server project.
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
using System.Security.Claims;
|
||||
using Aaru.Server.New.Data;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using Microsoft.AspNetCore.Components.Server;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Aaru.Server.New.Data;
|
||||
|
||||
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.
|
||||
internal sealed class IdentityRevalidatingAuthenticationStateProvider
|
||||
sealed class IdentityRevalidatingAuthenticationStateProvider
|
||||
(ILoggerFactory loggerFactory, IServiceScopeFactory scopeFactory, IOptions<IdentityOptions> options)
|
||||
: RevalidatingServerAuthenticationStateProvider(loggerFactory)
|
||||
{
|
||||
@@ -19,31 +19,25 @@ internal sealed class IdentityRevalidatingAuthenticationStateProvider
|
||||
AuthenticationState authenticationState, CancellationToken cancellationToken)
|
||||
{
|
||||
// Get the user manager from a new scope to ensure it fetches fresh data
|
||||
await using var scope = scopeFactory.CreateAsyncScope();
|
||||
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
||||
await using AsyncServiceScope scope = scopeFactory.CreateAsyncScope();
|
||||
|
||||
UserManager<ApplicationUser> userManager =
|
||||
scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
||||
|
||||
return await ValidateSecurityStampAsync(userManager, authenticationState.User);
|
||||
}
|
||||
|
||||
private async Task<bool> ValidateSecurityStampAsync(UserManager<ApplicationUser> userManager,
|
||||
ClaimsPrincipal principal)
|
||||
async Task<bool> ValidateSecurityStampAsync(UserManager<ApplicationUser> userManager, ClaimsPrincipal principal)
|
||||
{
|
||||
var user = await userManager.GetUserAsync(principal);
|
||||
ApplicationUser? user = await userManager.GetUserAsync(principal);
|
||||
|
||||
if(user is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if(!userManager.SupportsUserSecurityStamp)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
var principalStamp = principal.FindFirstValue(options.Value.ClaimsIdentity.SecurityStampClaimType);
|
||||
var userStamp = await userManager.GetSecurityStampAsync(user);
|
||||
if(user is null) return false;
|
||||
|
||||
return principalStamp == userStamp;
|
||||
}
|
||||
if(!userManager.SupportsUserSecurityStamp) return true;
|
||||
|
||||
string? principalStamp = principal.FindFirstValue(options.Value.ClaimsIdentity.SecurityStampClaimType);
|
||||
string userStamp = await userManager.GetSecurityStampAsync(user);
|
||||
|
||||
return principalStamp == userStamp;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user