Authorization requires a cascading parameter of type Task<AuthenticationState>. Consider using CascadingAuthenticationState to supply this.
means an AuthorizeView rendered with nothing above it supplying the authentication
state. In a dotnet new blazor app, add
builder.Services.AddCascadingAuthenticationState(); to Program.cs.
In the lab that one line turned a 500 into a page that said "You are not signed in.", with no other change to the template.
The error
curl.exe -s -i http://127.0.0.1:15621/account got HTTP/1.1 500 Internal Server Error
and the template's error page; the server console logged:
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request.
System.InvalidOperationException: Authorization requires a cascading parameter of type Task<AuthenticationState>. Consider using CascadingAuthenticationState to supply this.
...
Why it happens
The page was a new component in the template, rendered with static server-side rendering:
@page "/account"
@using Microsoft.AspNetCore.Components.Authorization
<PageTitle>Account</PageTitle>
<h1>Account</h1>
<AuthorizeView>
<Authorized>
<p>Signed in as @context.User.Identity?.Name.</p>
</Authorized>
<NotAuthorized>
<p>You are not signed in.</p>
</NotAuthorized>
</AuthorizeView>
AuthorizeView reads the current user from a cascading
Task<AuthenticationState> parameter. The template without authentication has
no CascadingAuthenticationState in Routes.razor and no registration
for one in Program.cs, so nothing supplied that parameter when the page rendered,
and the component threw. Only /account was requested in the test.
The fix
...
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddCascadingAuthenticationState();
...
After a rebuild, curl.exe -s -i http://127.0.0.1:15621/account answered
HTTP/1.1 200 OK and the page held <p>You are not signed in.</p>.
Routes.razor stayed as the template wrote it; the service registration was the
only change. Wrapping the router in CascadingAuthenticationState in
Routes.razor, with the registration removed again, also answered 200 with the same
paragraph in the test.
@using Microsoft.AspNetCore.Components.Authorization
<CascadingAuthenticationState>
<Router AppAssembly="typeof(Program).Assembly" NotFoundPage="typeof(Pages.NotFound)">
<Found Context="routeData">
<RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" />
<FocusOnNavigate RouteData="routeData" Selector="h1" />
</Found>
</Router>
</CascadingAuthenticationState>
The lab used one form at a time; both together were not tested. The page shows "not signed in" because this app has no sign-in at all; adding Identity or another scheme is a separate step.
How it was reproduced
dotnet new blazor -f net10.0 -o bz -n BzLab on .NET SDK 11.0.100, with no package
references and no authentication option (BzLab and Account.razor are the
test's names), plus the Account.razor page above. The built app was started
with dotnet bin\Debug\net10.0\BzLab.dll --urls http://127.0.0.1:15621 (Production
environment, so the response was the template's error page, not the developer exception page)
and the page requested with curl.exe. The console also logged the template's
"Failed to determine the https port for redirect" warning, which is unrelated.
Frequently asked
- How do I fix 'Authorization requires a cascading parameter of type Task<AuthenticationState>' in Blazor?
- Add builder.Services.AddCascadingAuthenticationState(); in Program.cs. In a dotnet new blazor app on .NET 10 that line alone made an AuthorizeView page render.
- Why does AuthorizeView throw in a Blazor app without authentication?
- AuthorizeView needs the authentication state as a cascading parameter. The template created without an authentication option supplies none, so the component threw when the page rendered.
- Where does the CascadingAuthenticationState error show up?
- In the server log as an InvalidOperationException from the exception handler middleware, and as a 500 with the error page in the response, which the lab read with curl while the app ran in Production.
The warning in the log has its own post, Failed to determine the https port for redirect. More decoded errors in the Fixes category.