dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.36k stars 9.99k forks source link

User Login Session could not maintain in Blazor Client Server Interactive Auto render mode. #56203

Closed manoj321 closed 4 months ago

manoj321 commented 4 months ago

Is there an existing issue for this?

Describe the bug

I have a blazor client server project configured with interactive auto mode for performance purpose and need to keep user login session, have tried session or cookies and found session and cookies can not be accessible in pre rendering mode as JS interop not available this time. I don't want to make server rendering to fix this. I have created user claim based identity and kept used details in it but on redirecting page to dashboard from user login user identity(authentication state) in my custom authentication class became reset. I have tried middleware things but still no luck, how MS has made it small things very complicated in blazor.

Expected Behavior

either user authentication state in my custom authentication provider class should persist and session or cookies should be accessible in pre rendering as I have to show user based data on page load without showing any loader there. state only persists if I kept it as singleton but it should be scoped or transient to make it user wise session data.

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

.Net Core 8 Blazor Razor pages

Anything else?

No response

javiercn commented 4 months ago

@manoj321 thanks for contacting us.

It's not clear to us what you are trying to achieve. When you talk about session, do you mean window.sessionStorage?

Assuming that's the case, that will never be available during prerendering.

If you have setup a cookie for your client, then you should be able to access it during prerendering through the HttpContext.

manoj321 commented 4 months ago

create cookie is also not supporting in interactive mode pre rendering, please suggest the correct way to achieve maintain user login session state in pre rendering so same details will be available on next page under onintializedasync event of razor page.

manoj321 commented 4 months ago

I could set the cookie using client js script but after redirection on next page cookie is not present there in headers.

manoj321 commented 4 months ago

I am trying to set cookie using js on login button click and trying to get same on next page in oninitializedasync event , this cookie should be persistent unless user logs out. so I can access user details using cookie anywhere on different pages in oninitializedasync event.

manoj321 commented 4 months ago

please guide me on this I am stuck from last 8 days.

javiercn commented 4 months ago

@manoj321 thanks for the additional details.

How are you setting the cookie, is it for the same domain? You mention a redirect, and maybe what you are trying to accomplish is not possible.

Keep in mind that you can't access any form of storage across domains, so depending on what you are trying to accomplish it might not be possible, or you might need to find a different way.

manoj321 commented 4 months ago

Thanks team, I could now create, access, authenticate and remove my application user cookie from client side js, thanks for your suggestion. this one is the only integration works with blazor interactive auto render mode client server app to access data on onitializedasync event of razor pages. I created and removed from client side and accessing/authenticate at server end. Thanks.