C
C#•2mo ago
jiheielf

Blazor Auto render mode authentication using external ASP.NET web api with ASP.NET Authentication

I'm a new guy into Blazor Authentication. I have tried to create an auto render mode blazor application, and because this is using external web api project (ASP.NET web api project) so the default individual template would not suit the requirement. Then I have tried to learn from official documents, stackover flow and some ai tools, so I got my template application on my git repo "https://github.com/TokisakiSusumu/AutoRenderDemoApiFromClient" so basically it is using its own Auth provider and Auth services for both SSR and CSR and both of them will point to the controllers in the server project and then it sends all the authentication api call to the backend web api which contains the dbcontext of the actual ASP.NET Identity tables. Currently it is kind of working but not in the normal way and I can't figure it out anymore and want some help. Currently the Login page in the Client project can only use @rendermode @(new InteractiveWebAssemblyRenderMode(prerender: false)), if it uses @rendermode InteractiveWebAssembly then it would give the error as the screenshot shows. And if it is @rendermode InteractiveAuto, then the SSR basically won't work, what will happen is first time (SSR) it tries to login, api seems went through but I don't have the authentication state, so it will redirect me to the login page again (this time it is CSR), then it will work. and I have no idea why this happen. As well as the log out method, currently it is a form in the server project and the controller will then send a return LocalRedirect("~/"); back to be able to make the whole work flow of log out work, but I'm not sure if this is the correct way of doing it. Please teach me the correct way of doing it and tell me what actaully is going on under the hood and why it is not working please.🥹 The web api Im using is just "https://github.com/TokisakiSusumu/AuthenticationTest" which is using the local sql server db and the MapIdentityApi methods for all the authentication controllers.
No description
1 Reply
jiheielf
jiheielfOP•2mo ago
The log out form is in the MainLayout.razor. I'm also new into auto render mode, so wonder what is the correct way to implement both login and logout as a auto render mode blazor project Probably this is not even the correct approach, just found out that you can request cookie from asp.net identity instead of getting bearer token...

Did you find this page helpful?