Auth pages with auto render mode in blazor

Mahdi Elahi 31 Reputation points
2025-04-13T16:46:18.7+00:00

I want to build a Blazor 8 or 9 project using Interactive Auto .
But I want to implement authentication using JWT or identity , so that whenever I want, I can show the login page inside a modal or elsewhere.( without refresh page or ... )

Because I've tested it before — when using a modal and trying to use SignInManager.SignIn inside the login component, it throws a 'headers are read-only, response has already started ' error and doesn't let it work.

I've seen many websites that show auth modals anywhere without refreshing the page, and it works fine.
how to do it ?

Blazor Training
Blazor Training
Blazor: A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.Training: Instruction to develop new skills.
31 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 75,051 Reputation points
    2025-04-14T16:15:42.6666667+00:00

    interactive auto is a server static render pre-render with a WASM client. if login is to be handled by the WASM client without a redirect, then you need to write custom authentication.

    the build-in authentication uses razor pages and cookie authentication. the Blazor app redirects to the razor pages to login and set the cookie. the SSR renders to the client via pre-render persist state the user principal:

    https://learn.microsoft.com/en-us/aspnet/core/blazor/components/prerender?view=aspnetcore-9.0

    the other option with WASM is oauth and msal which requires requires the Blazor code redirect to the oauth server to get the id token.

    if you are using individual accounts, you can write custom authenication. your SSR server will need to supply a webapi to login via a passed user name & password, and return the user principal in some format. if the WASM will make other api calls, then a JWT token would be handy, but you could use a custom header.

    the Blazor login page would call the webapi to login, then call the custom authentication state provider and pass the new user principal information.

    note: Blazor WASM does not support SigninManager. it needs to call a webapi. If you switched to Blazor Server you could use it.

    0 comments No comments

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.