Table of Contents

Class ApplicationContextCircuitHandler

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

Populates the circuit-scoped IApplicationContext for Interactive Server components.

An Interactive Server component runs in a SignalR circuit that owns its own DI scope, distinct from the HTTP request scope where AppPrincipalTransform (and, during prerendering, AppPrincipalServerAuthenticationStateProvider) populate the request-scoped IApplicationContext. That request scope is disposed once the response is sent, so the circuit starts with an empty IApplicationContext and component event handlers (e.g. a button click on the Counter page) see a null Principal.

This handler resolves the circuit-scoped AuthenticationStateProvider once the SignalR connection is up and builds the AppPrincipal into the circuit-scoped IApplicationContext, so it is available for the whole lifetime of the circuit — independent of whether/when the render pipeline re-invokes GetAuthenticationStateAsync.

It also follows the changes of the circuit's authentication state: when the circuit reconnects, Blazor sets the user of the new connection after the OnConnectionUpAsync(Circuit, CancellationToken) handlers ran, and a revalidating provider can turn the state anonymous. Nothing reads the state again after such a change, so the handler rebuilds the principal from the new state, or clears it for an anonymous user.

[Export(typeof(CircuitHandler))]
[Scoped]
public sealed class ApplicationContextCircuitHandler : CircuitHandler, IDisposable
Inheritance
ApplicationContextCircuitHandler
Implements
Inherited Members

Constructors

ApplicationContextCircuitHandler(AuthenticationStateProvider, IClaimAuthorizationFiller, IClaimProfileFiller, IApplicationContext)

Initializes a new instance of the ApplicationContextCircuitHandler class.

public ApplicationContextCircuitHandler(AuthenticationStateProvider authenticationStateProvider, IClaimAuthorizationFiller authorizationFiller, IClaimProfileFiller profileFiller, IApplicationContext applicationContext)

Parameters

authenticationStateProvider AuthenticationStateProvider

The circuit-scoped authentication state provider.

authorizationFiller IClaimAuthorizationFiller

The filler that builds the authorization from the claims.

profileFiller IClaimProfileFiller

The filler that builds the user profile from the claims.

applicationContext IApplicationContext

The circuit-scoped application context that receives the principal.

Methods

Dispose()

Stops following the authentication state of the circuit.

public void Dispose()

OnConnectionUpAsync(Circuit, CancellationToken)

Builds the AppPrincipal of the authenticated user and sets it in the circuit-scoped IApplicationContext when the circuit connection is up. For an anonymous user, the principal is cleared.

public override Task OnConnectionUpAsync(Circuit circuit, CancellationToken cancellationToken)

Parameters

circuit Circuit

The circuit.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task

A task that completes when the principal is set.