Blazor.Auth
Blazor.Auth copied to clipboard
Infinite Loop When Calling AuthenticationStateProvider.GetAuthenticationStateAsync() in OnInitializedAsync with AuthorizeRouteView in Routes.razor
Infinite Loop When Calling AuthenticationStateProvider.GetAuthenticationStateAsync() in OnInitializedAsync with AuthorizeRouteView in Routes.razor
To reproduce this issue, create a new Blazor Server App with <Routes @rendermode="InteractiveServer" /> in App.razor, and add the following code to Home.razor:
protected override async Task OnInitializedAsync()
{
// The following line causes the page to reload indefinitely
var authenticationState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var claimsPrincipal = authenticationState.User;
}
Also, configure AuthorizeRouteView in Routes.razor like this:
@using Microsoft.AspNetCore.Components.Authorization
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" />
<FocusOnNavigate RouteData="routeData" Selector="h1" />
</Found>
</Router>
Note: If I replace the call to AuthenticationStateProvider.GetAuthenticationStateAsync() with UserService.GetAuthenticationStateAsync(), the issue does not occur and everything works as expected. Is it safe to use UserService in this scenario instead?