繁体   English   中英

OpenIddict:如何手动检查访问令牌并获取身份

[英]OpenIddict: How to manually check access token and get identity

我正在使用OpenIddict进行身份验证/授权。
我需要手动检查访问令牌,并在该令牌后面获取用户(ClaimsPrincipal)。 怎么样?

用例:
我正在使用SignalR。 在来自客户端的每个方法调用上,我都想检查用户是否已通过身份验证。 我的计划是从Angular发送访问令牌并在Hub方法中检查它。 当我在Controller上使用[Authorize]属性时,基本上必须发生相同的事情。

假设此问题与如何使用JWT授权SignalR Core Hub方法有关 ,我不建议您自己解密OpenIddict发出的不透明访问令牌。

如果您真的想自己做,则可以使用TicketDataFormat使用的ASP.NET Core数据保护“用途字符串”手动实例化TicketDataFormat实例:

// Resolve the data protection provider from the DI container.
// Depending on where this snippet is used, you must be able
// to directly use constructor injection (e.g in a controller).
var provider = app.ApplicationServices.GetRequiredService<IDataProtectionProvider>();

var protector = provider.CreateProtector(
    nameof(OpenIdConnectServerHandler),
    nameof(OpenIdConnectServerOptions.AccessTokenFormat),
    OpenIdConnectServerDefaults.AuthenticationScheme);

var format = new TicketDataFormat(protector);

// If the access token is not malformed, a non-null value
// is returned. Note that you'll have to manually validate
// the expiration date and the audience of the ticket.
var ticket = format.Unprotect("your access token");

暂无
暂无

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

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