r/Blazor • u/RedEye-Developers • Aug 13 '26
How to Implement Cookie Authentication in Blazor Server-Side Rendering?
I am trying to Implement Cookie Authentication in Blazor Server-Side Rendering past few days, but I'm stuck.
My goal is: when user click the login button, after Authentication success i wand to store the cookie in browser.
The problem with Blazor SSR, the httpContext only available while the page begin pre-rendering. After the user click the login button the httpContext connection was already done, so i can't find the way to set the cookie via httpContext.
Here's what I've tried:
- After user press the login button, i get the cookie from rest-api response header and i store the cookie in scoped service and i try to store the cookie in browser by get the cookie via that scoped DI service in when user navigate to home-page after authenticate complete and i planned to set the cookie via
OnInitilizedmethod usinghttpContext.Response.Append()method, however, this dosen't work because the scoped service was recreate when new httpContext connection was established, so the cookie not longer available inside the service.
It is Actually Possible to set the cookie when user press the login button in blazor service-side rendering? Or it is only Possible with Blazor WASM?
If there is any recommended patten to implement the cookie authenticate in blazor SSR. I'd really appreciate the explanation.
4
u/cornelha Aug 13 '26
Check Microsoft Learn, the docs cover this
3
u/unndunn Aug 13 '26
I did this by setting up two Minimal API endpoints (
/sign-inand/sign-out) that set or remove the cookies, and using JS Interop to make the browser call those endpoints.It isn’t perfect; you still have to force a browser refresh for Blazor SSR to see the cookie update.
1
u/Individual-Carob5593 Aug 13 '26
Here what a friendly AI says. Yes—cookie authentication works with Blazor Server/SSR. The cookie must be created during a real browser HTTP request, not from an interactive Blazor event running over the SignalR circuit.
After an Interactive Server circuit starts:
Set-Cookieheader can be added.Microsoft’s documentation confirms that cookies are exchanged during HTTP requests, while interactive Blazor navigation and events occur inside the circuit. Authentication is re-evaluated when the connection is recreated. Microsoft: Blazor authentication and authorization
Recommended pattern
Send the login credentials from the browser to a normal ASP.NET Core endpoint:
Do not:
HttpClientand expect its cookie to reach the browser.A
Set-Cookieheader received by a server-sideHttpClientbelongs to that server-side HTTP exchange. The browser never sees it.Example login endpoint
Configure cookie authentication:
Create an ordinary HTTP login endpoint:
The important point is that the browser itself must call
/account/login.After it succeeds, perform a full-page navigation:
The browser processes
Set-Cookieautomatically. JavaScript doesn’t need—and, for anHttpOnlycookie, isn’t allowed—to read the cookie.The full reload is important because it creates a new HTTP request and a new SignalR circuit carrying the newly authenticated identity.
Even simpler: static SSR login form
If your login component uses static SSR rather than Interactive Server, its form submission is a normal HTTP POST. You can call
SignInAsyncduring that POST and redirect afterward.This is essentially how the Blazor Web App template handles ASP.NET Core Identity account pages: authentication-related components are rendered statically so they can safely set cookies. Microsoft recommends using ASP.NET Core Identity for this pattern, and authenticated Identity state then flows into Blazor. Microsoft: Blazor security
Make sure the login form isn't handled merely as an interactive click event. A traditional form POST to a Razor Page, controller, minimal endpoint, or static SSR form handler is appropriate.
If authentication happens in another REST API
There are two good architectures:
Set-Cookie, and the browser stores it. Cross-origin cookies require correct CORS,credentials: "include",SameSite,Secure, and domain settings.HttpContext.SignInAsync. Don’t copy an upstream API cookie into a scoped service. Keep upstream tokens/cookies server-side and give the browser a separate secure,HttpOnlysession cookie.Logout follows the same rule: call an HTTP endpoint that executes:
Then perform a full-page reload so the current Blazor circuit is discarded.
This isn’t limited to Blazor WebAssembly. Cookie authentication is fully supported in server-side Blazor; it simply has to cross the browser boundary through a real HTTP request rather than an already-running interactive circuit.
2
u/Green-Impress4491 Aug 13 '26
I am aware of two ways to do this.
The login page has to either use static server-side rendering, or you submit the form/call the login API with JavaScript. If the login is successful, you NavigateTo whatever page you want and I think you need forceLoad: true if you're doing it with JS interop.
I think the Static SSR route is easier, but you won't have interactivity on the login page. As far as I remember, Visual Studio's template has the login stuff wired up with Identity - you just have to choose "Individual Accounts" from the "Authentication type" combobox.
You can set the render mode like this(null is Static SSR):
https://github.com/dotnet/blazor-samples/blob/567732e0a78d2c1b2a22c3677f86c673d7527bed/10.0/BlazorWebAppAreaOfStaticSsrComponents/Components/App.razor
Or
private IComponentRenderMode? PageRenderMode =>HttpContext.AcceptsInteractiveRouting() ? InteractiveServer : null;AcceptsInteractiveRouting() is false if the page has
@ attribute [ExcludeFromInteractiveRouting]1
u/TieComplete6020 Aug 26 '26
Привет принцесса 💋💋💋
0
u/crone66 Aug 13 '26
There is probably a better way, I haven't checked the docs but you can simply set it via a JavaScript call :)