Class AppPrincipalServerAuthenticationStateProvider
Server (Interactive Server / SignalR circuit) counterpart of the WebAssembly
Arc4u.Blazor.AppPrincipalFromAuthenticationState used on the client.
On the client, the AppPrincipal is rebuilt from the serialized
authentication state through the DeserializationCallback and pushed into the
circuit-scoped IApplicationContext.
On the server, AppPrincipalTransform (an IClaimsTransformation) runs
in the HTTP request scope during UseAuthentication(), so the IApplicationContext
it populates belongs to that request scope and is discarded before the interactive circuit runs.
The circuit uses its own DI scope, whose IApplicationContext is therefore empty.
This provider closes that gap: it derives from Microsoft.AspNetCore.Components.Server.ServerAuthenticationStateProvider
(which the Blazor endpoint seeds from HttpContext.User) and, whenever the authentication
state is resolved inside the circuit, rebuilds the AppPrincipal and sets it on the
circuit-scoped IApplicationContext — mirroring the client behaviour.
When the state changes (for example when the circuit reconnects with another user or without a user), Blazor forwards the new state without calling GetAuthenticationStateAsync() again, so the provider also updates the IApplicationContext on AuthenticationStateChanged.
[Export(typeof(AuthenticationStateProvider))]
[Scoped]
public sealed class AppPrincipalServerAuthenticationStateProvider : ServerAuthenticationStateProvider, IHostEnvironmentAuthenticationStateProvider
- Inheritance
-
ServerAuthenticationStateProviderAppPrincipalServerAuthenticationStateProvider
- Implements
- Inherited Members
Constructors
AppPrincipalServerAuthenticationStateProvider(IClaimProfileFiller, IClaimAuthorizationFiller, IApplicationContext)
Initializes a new instance of the AppPrincipalServerAuthenticationStateProvider class.
public AppPrincipalServerAuthenticationStateProvider(IClaimProfileFiller claimsFiller, IClaimAuthorizationFiller authorizationFiller, IApplicationContext applicationContext)
Parameters
claimsFillerIClaimProfileFillerThe filler that builds the user profile from the claims.
authorizationFillerIClaimAuthorizationFillerThe filler that builds the authorization from the claims.
applicationContextIApplicationContextThe circuit-scoped application context that receives the principal.
Methods
GetAuthenticationStateAsync()
Gets the authentication state and, for an authenticated user, sets the AppPrincipal in the circuit-scoped IApplicationContext. For an anonymous user, the principal of the IApplicationContext is cleared.
public override Task<AuthenticationState> GetAuthenticationStateAsync()
Returns
- Task<AuthenticationState>
The authentication state, whose user is the AppPrincipal when the user is authenticated.