Table of Contents

Class AppPrincipalServerAuthenticationStateProvider

Namespace
Arc4u.Blazor
Assembly
Arc4u.OAuth2.AspNetCore.Blazor.dll

Server (Interactive Server / SignalR circuit) counterpart of the WebAssembly Arc4u.Blazor.AppPrincipalFromAuthenticationState used on the client: it sets the user's AppPrincipal in the circuit, and ends the circuit's sign-in when the user's sign-in has ended.

[Export(typeof(AuthenticationStateProvider))]
[Scoped]
public sealed class AppPrincipalServerAuthenticationStateProvider : RevalidatingServerAuthenticationStateProvider, IHostEnvironmentAuthenticationStateProvider, IDisposable
Inheritance
ServerAuthenticationStateProvider
AppPrincipalServerAuthenticationStateProvider
Implements
Inherited Members

Remarks

On the server, AppPrincipalTransform (an IClaimsTransformation) runs in the HTTP request scope during UseAuthentication(), so the IApplicationContext it populates belongs to that request scope. The circuit uses its own DI scope, whose IApplicationContext is empty. This provider (which the Blazor endpoint seeds from HttpContext.User) rebuilds the AppPrincipal whenever the authentication state is read, and on every AuthenticationStateChanged (a reconnection with another user or without a user), and sets it on the scope's IApplicationContext.

Every Interval (two minutes by default) it also asks the ISignInValidator whether the sign-in of an authenticated circuit still exists: by default SessionStoreSignInValidator, which checks the cookie's session store. When the sign-in has ended (sign-out in another tab, expired session), or the check throws, Blazor moves the circuit to an anonymous state. An anonymous visitor is never checked.

Whenever the state turns anonymous, the user is removed: the principal of the circuit, and the principal and tokens (TokenRefreshInfo) of the SignalR connection's request scope, which the JwtHttpHandler and the gRPC interceptor read. Every operation the stale page starts from then on finds no user, and no API is called with the user's token. Components that read the authentication state (AuthorizeView, [Authorize] pages rendered interactively) render their anonymous content.

Register it with AddAppPrincipalCircuitRevalidation: that call also wraps the cookie's session store in a SessionKeyTicketStore, without which the principals carry no session key and the default validator cannot check anything (it then logs a warning once, and the circuits keep their user until they reconnect). Set Enabled to false to turn the check off.

Constructors

AppPrincipalServerAuthenticationStateProvider(IClaimProfileFiller, IClaimAuthorizationFiller, IApplicationContext)

Initializes a new instance of the AppPrincipalServerAuthenticationStateProvider class without revalidation and without access to the scope's services: the circuit keeps its user until it reconnects. Prefer the constructors used by the dependency injection.

public AppPrincipalServerAuthenticationStateProvider(IClaimProfileFiller claimsFiller, IClaimAuthorizationFiller authorizationFiller, IApplicationContext applicationContext)

Parameters

claimsFiller IClaimProfileFiller

The filler that builds the user profile from the claims.

authorizationFiller IClaimAuthorizationFiller

The filler that builds the authorization from the claims.

applicationContext IApplicationContext

The circuit-scoped application context that receives the principal.

AppPrincipalServerAuthenticationStateProvider(ILoggerFactory, IClaimProfileFiller, IClaimAuthorizationFiller, IApplicationContext, ISignInValidator?, IOptions<CircuitRevalidationOptions>, IServiceProvider?)

Initializes a new instance of the AppPrincipalServerAuthenticationStateProvider class.

public AppPrincipalServerAuthenticationStateProvider(ILoggerFactory loggerFactory, IClaimProfileFiller claimsFiller, IClaimAuthorizationFiller authorizationFiller, IApplicationContext applicationContext, ISignInValidator? signInValidator, IOptions<CircuitRevalidationOptions> options, IServiceProvider? scopeServices)

Parameters

loggerFactory ILoggerFactory

The logger factory.

claimsFiller IClaimProfileFiller

The filler that builds the user profile from the claims.

authorizationFiller IClaimAuthorizationFiller

The filler that builds the authorization from the claims.

applicationContext IApplicationContext

The scoped application context that receives the principal.

signInValidator ISignInValidator

Tells whether the sign-in still exists; null turns the revalidation off.

options IOptions<CircuitRevalidationOptions>

The revalidation options.

scopeServices IServiceProvider

The services of the scope (circuit or request), used to remove the user's tokens, and the user of the connection's request scope, when the state turns anonymous.

Exceptions

ArgumentOutOfRangeException

The revalidation is enabled and Interval is not more than zero and at most MaxInterval.

AppPrincipalServerAuthenticationStateProvider(ILoggerFactory, IClaimProfileFiller, IClaimAuthorizationFiller, IApplicationContext, IServiceProvider)

Initializes a new instance of the AppPrincipalServerAuthenticationStateProvider class with the ISignInValidator and the CircuitRevalidationOptions registered in scopeServices; when no validator is registered, a SessionStoreSignInValidator is used, and the default options when none are configured.

public AppPrincipalServerAuthenticationStateProvider(ILoggerFactory loggerFactory, IClaimProfileFiller claimsFiller, IClaimAuthorizationFiller authorizationFiller, IApplicationContext applicationContext, IServiceProvider scopeServices)

Parameters

loggerFactory ILoggerFactory

The logger factory.

claimsFiller IClaimProfileFiller

The filler that builds the user profile from the claims.

authorizationFiller IClaimAuthorizationFiller

The filler that builds the authorization from the claims.

applicationContext IApplicationContext

The scoped application context that receives the principal.

scopeServices IServiceProvider

The services of the scope (circuit or request).

Properties

RevalidationInterval

Gets the interval between revalidation attempts.

protected override TimeSpan RevalidationInterval { get; }

Property Value

TimeSpan

Methods

Dispose(bool)

protected override void Dispose(bool disposing)

Parameters

disposing bool

GetAuthenticationStateAsync()

Gets the authentication state and, for an authenticated user, sets the AppPrincipal in the scope's IApplicationContext. For an anonymous user, the user is removed.

public override Task<AuthenticationState> GetAuthenticationStateAsync()

Returns

Task<AuthenticationState>

The authentication state, whose user is the AppPrincipal when the user is authenticated.

ValidateAuthenticationStateAsync(AuthenticationState, CancellationToken)

Determines whether the authentication state is still valid.

protected override Task<bool> ValidateAuthenticationStateAsync(AuthenticationState authenticationState, CancellationToken cancellationToken)

Parameters

authenticationState AuthenticationState

The current AuthenticationState.

cancellationToken CancellationToken

A CancellationToken to observe while performing the operation.

Returns

Task<bool>

A Task that resolves as true if the authenticationState is still valid, or false if it is not.