C# 如何在使用WebSocket的ASP.NET Core 3.1的长寿命管道中检测承载令牌过期?
我的SPA客户端在websocket连接上发送C# 如何在使用WebSocket的ASP.NET Core 3.1的长寿命管道中检测承载令牌过期?,c#,websocket,identityserver4,asp.net-core-3.1,bearer-token,C#,Websocket,Identityserver4,Asp.net Core 3.1,Bearer Token,我的SPA客户端在websocket连接上发送access\u令牌。然后通过调用httpContext.authenticateSync()在API上对其进行身份验证设置为接收到httpContext.User的ClaimsPrincipal 让我们想象一下,我们总是可以从当前的HttpContext.Items字典中获取新的access\u令牌(因为它是由websocket消息填充的) 需要知道,如何在API端检测当前承载何时过期,并执行以下操作之一: 还原当前HttpContext的“已验
access\u令牌
。然后通过调用httpContext.authenticateSync()在API上对其进行身份验证代码>设置为接收到httpContext.User的ClaimsPrincipal
让我们想象一下,我们总是可以从当前的HttpContext.Items
字典中获取新的access\u令牌(因为它是由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 =>
{
// ...
});
它使用“ASP.NET核心中的灵活访问令牌验证”原则,如中所述