authenticationblazorblazor-webappstate-persistent

Transferring Authentication State from Prerendering mode to Interactive Rendering mode in Blazor WebApp


In Blazor 9, it seems that AddAuthenticationStateSerialization() and AddAuthenticationStateDeserialization() only work when using ASP.NET Core Identity in the server project (as in the Visual Studio template). For instance, when we intend to use a remote API for authentication with JWT, this automatic persistent serialization does not appear to be helpful. As a result, we need to create a custom AuthenticationStateProvider again to persist the JWT between prerendering and interactive rendering modes.

Since my login page is in prerendering mode, I am looking for JWT storage solutions that do not rely on local storage or cookies.

Does anyone have a suggestion or solution for this?


Solution

  • I figured out my problem using the approach I intended. Now I can handle user authentication using an external API that returns a JWT (access token) upon successful authentication. The login Razor page of my Blazor WebApp project (in auto-rendering mode) is a static SSR page. The authentication state is then transferred to the interactive rendering mode using the new Blazor 9.0 APIs: AddAuthenticationStateSerialization() and AddAuthenticationStateDeserialization().

    Here is my solution as a minimal repo, which demonstrates how these features can be implemented.