如何解决如何使用WebSocket在ASP.NET Core 3.1的长寿命管道中检测Bearer令牌到期?
我的SPA客户端发送access_token
作为Websocket连接上的第一条消息。然后通过调用httpContext.AuthenticateAsync();
在API上进行身份验证,并将接收到的ClaimsPrincipal设置为httpContext.User。
让我们想象一下,我们总是可以从当前的access_token
字典中获得新鲜的HttpContext.Items
(因为它是从websocket消息中填充的)。
需要知道如何检测当前承载何时在API端过期并执行以下操作之一:
- 还原当前HttpContext的“已认证”状态(如果可能)
- 关闭websocket以强制客户端重新连接。
如果有新令牌,是否可以在旧令牌过期之前用新令牌替换旧令牌?
API具有三种身份验证方案。在我的情况下,它总是选择自省。
services.AddAuthentication(x =>
{
x.DefaultScheme = ApiAuthenticationSchemes.DefaultScheme;
x.DefaultAuthenticateScheme = ApiAuthenticationSchemes.DefaultScheme;
})
// Stub to not start process before protocol being upgraded to websocket
// as there is no bearer in header.
.AddJwtBearer(ApiAuthenticationSchemes.WebsocketScheme,default)
.AddJwtBearer(ApiAuthenticationSchemes.DefaultScheme,x =>
{
// ...
// Switching scheme by condition.
// Three stages - stub before protocol upgraded,interceptor if there
// is no dot in token (reference),current scheme,if there is
// just regular access token.
x.ForwardDefaultSelector = ApiAuthenticationSchemes.ForwardWebsocket();
// ...
})
.AddOAuth2Introspection(ApiAuthenticationSchemes.IntrospectionScheme,x =>
{
// ...
});
它使用了article中所述的“ ASP.NET Core中的灵活访问令牌验证”的原理。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。