When can Task<AuthenticationState> be null in Blazor Server? #69022
Replies: 1 comment
|
With The registration creates a So this is reasonable when the cascade is guaranteed by the application: @code {
[CascadingParameter]
private Task<AuthenticationState> AuthenticationStateTask { get; set; } = default!;
protected override async Task OnInitializedAsync()
{
var state = await AuthenticationStateTask;
await LoadForTenantAsync(GetTenantId(state.User));
}
}The nullable annotation commonly seen in framework components represents the case where there is no matching cascade. It is not an indication that the registered provider normally supplies a null task. A custom provider returning null would also be violating the API contract. A missing There is a separate reason you may still need In short:
|
Uh oh!
There was an error while loading. Please reload this page.
In Blazor Server, if I use
builder.services.AddCascadingAuthenticationState(), will theTask<AuthenticationState>?cascading parameter ever actually benull?I'm asking because I'm working on a multi-tenant application, and most pages need to fetch data based on the user's claims. Because of the
nullcheck, I end up usingOnParametersSetAsyncwith comparisons a lot, when I would prefer to just load the data inOnInitializedAsyncand be done with it.Looking at the AspNetCore source code, it seems like the default
AuthenticationStateProviderimplementations all seem to have a non-nullvalue.All reactions