简体   繁体   English

Blazor - 服务器端 - Cookie 身份验证

[英]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.我想在 Blazor 服务器端实现身份验证,使用 cookie 身份验证,我检查了很多示例项目,但它们都使用 Microsoft.Identity。

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. cookie auth 和服务器端 Blazor 的问题在于,服务器端 Blazor 用于与服务器对话的 web 套接字将在 cookie 过期后很长时间保持打开和连接。 There might be a way to end/refresh this websocket periodically, but I didn't look into it.可能有一种方法可以定期结束/刷新这个 websocket,但我没有研究它。

If your actually using Blazor WebAssembly you have to implement your own AuthenticationStateProvider that manages your login/logout.如果您实际使用 Blazor WebAssembly,则必须实现自己的AuthenticationStateProvider来管理您的登录/注销。

You'll also want to implement a /user/keepalive endpoint so that when a user navigates their cookie is kept alive.您还需要实现 /user/keepalive 端点,以便在用户导航时保持其 cookie 处于活动状态。 See Blazor's NavigationManager.LocationChanged请参阅 Blazor 的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.还有一个 /user/me 端点来检测 cookie 是否仍然有效,并在用户按 F5 时加载任何权限。 Nothing more annoying than logging in, hitting F5 and having to login again.没有什么比登录、按 F5 并不得不再次登录更烦人的了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM