C
C#3y ago
malkav

Blazor Authentication passes GetAuthenticationStateAsync() three times

So I've been trying to find a solution, and I don't even know where to start anymore. But here goes... I've got a Blazor WASM project, in which I have a Microsoft Authentication Login thing set-up. In localhost I see the Console.WriteLine( being called three times from the same function. The function that does this:
private async Task GetClaimsPrincipalData()
{
AuthenticationState authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();

if (authState.User.Identity.IsAuthenticated)
{
_authMessage = $"{authState.User.Identity.Name} is authenticated";
_claims = authState.User.Claims.Where(x => returnClaims.Contains(x.Type));
}
else
{
_authMessage = "The user is not authenticated";
}
}
private async Task GetClaimsPrincipalData()
{
AuthenticationState authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();

if (authState.User.Identity.IsAuthenticated)
{
_authMessage = $"{authState.User.Identity.Name} is authenticated";
_claims = authState.User.Claims.Where(x => returnClaims.Contains(x.Type));
}
else
{
_authMessage = "The user is not authenticated";
}
}
I was told awaiting the GetAuthenticationStateAsync() is a common mistake, but if I use .Result instead of await I get the following error in my browser Failed to fetch which has to do with my GraphClient, so I set that to .Result instead of await too, and then I get Cannot wait on monitors on this runtime So basically I am at an issue where I'm not even sure how to start resolving it. Localhost follows this flow: login > homepage > redirect url > homepage Live view does the same but different: login > homepage > redirect url > 404 not found So somewhere in my authentication flow I'm doing something wrong, and I am not entirely sure where to start looking for the issue. Can someone please help me out here? I've tried Blazor School, but the owner became very toxic, and he linked the following page: https://blazorschool.com/tutorial/blazor-wasm/dotnet6/implementing-authentication-829424 which confuses me even more...
1 Reply
malkav
malkavOP3y ago
Resolved the issue! Apparently I didn't have a staticwebapp.config.json in my client's root folder yet. The contents of which are as follows (for if anyone else has the same issue)
{
"navigationFallback": {
"rewrite": "/index.html"
}
}
{
"navigationFallback": {
"rewrite": "/index.html"
}
}

Did you find this page helpful?