How to fix "GetAuthenticationStateAsync was called before SetAuthenticationState" in Blazor MAUI project ?

Boot Dat 1 Reputation point
2022-06-05T21:34:52.537+00:00

I already have a Blazor Server application with authentication type "individual accounts". Everything is working.
Now i want to try out MAUI, so i created a razor class library in the solution and moved all logic from BlazorServer to the Razor Class Library project.
Fixed HTML styling and so on so the application runs as before. Everything works good including authentication.
Now, i added MAUI Blazor project to the solution, run the MAUI application and it run as expected.
Now, I referenced the Razor Class Library Project. Tried displaying the UI from RazorClassLibrary by running the RazorClassLibrary.App from MauiBlazor's Main.razor.

Now getting the error:

GetAuthenticationStateAsync was called before SetAuthenticationState.
at Microsoft.AspNetCore.Components.Server.ServerAuthenticationStateProvider.GetAuthenticationStateAsync()
at Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState.OnInitialized()
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

How can i fix this ?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,611 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,596 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,592 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.