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.