Youssef Salame
Youssef Salame

Reputation: 11

Blazor - Server Side - Cookie Authentication

I want to implement the Authentication in Blazor Server Side, using the cookie Authentication, I have checked a lot of sample projects, but all of them are using Microsoft.Identity.

Upvotes: 0

Views: 1249

Answers (1)

jhr
jhr

Reputation: 703

The problem with cookie auth and server-side Blazor, is that the web socket that server-side Blazor uses to talk to the server will remain open and connected long after the cookie expires. There might be a way to end/refresh this websocket periodically, but I didn't look into it.

If your actually using Blazor WebAssembly you have to implement your own AuthenticationStateProvider that manages your login/logout.

You'll also want to implement a /user/keepalive endpoint so that when a user navigates their cookie is kept alive. See Blazor's NavigationManager.LocationChanged

As well as a /user/me endpoint to detect if the cookie is still valid, and load any permissions when a user hits F5. Nothing more annoying than logging in, hitting F5 and having to login again.

Upvotes: 1

Related Questions