Init
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
|
||||
namespace Prefab.Tests.Infrastructure;
|
||||
|
||||
/// <summary>
|
||||
/// Simplified authentication provider for tests. Returns a configurable principal and works outside Blazor circuits.
|
||||
/// </summary>
|
||||
public sealed class TestAuthenticationStateProvider : AuthenticationStateProvider
|
||||
{
|
||||
private readonly object _sync = new();
|
||||
private ClaimsPrincipal _principal = CreateAnonymous();
|
||||
|
||||
public override Task<AuthenticationState> GetAuthenticationStateAsync()
|
||||
{
|
||||
ClaimsPrincipal principal;
|
||||
lock (_sync)
|
||||
{
|
||||
principal = _principal;
|
||||
}
|
||||
|
||||
return Task.FromResult(new AuthenticationState(principal));
|
||||
}
|
||||
|
||||
public void SetPrincipal(ClaimsPrincipal? principal)
|
||||
{
|
||||
lock (_sync)
|
||||
{
|
||||
_principal = principal ?? CreateAnonymous();
|
||||
}
|
||||
|
||||
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(_principal)));
|
||||
}
|
||||
|
||||
public void SetAnonymous() => SetPrincipal(null);
|
||||
|
||||
private static ClaimsPrincipal CreateAnonymous() => new(new ClaimsIdentity());
|
||||
}
|
||||
Reference in New Issue
Block a user