asp.net – 如何使用JwtSecurityTokenHandler和JWKS端点验证JWT?
|
我正在使用IdentityServer4来保护多个服务的原型,但需要注意的是,这些服务很可能不会被迁移(在可预见的未来)以使用ASP.NET Core的OWIN中间件习惯用法.因此,我不能利用许多中间件助手来自动验证JWT,只需提供IdentityServer的着名JWKS端点等等. 如果我能重建这种行为会很好,我想尽可能利用微软的 JwtSecurityTokenHandler使用 ClaimsPrincipal ValidateJwt(string token,IdentityModel.Client.DiscoveryResponse discovery)
{
JwtSecurityToken jwt = new JwtSecurityToken(token);
TokenValidationParameters validationParameters = new TokenValidationParameters
{
ValidateAudience = true,ValidateIssuer = true,RequireSignedTokens = true,ValidIssuer = "expected-issuer",ValidAudience = "expected-audience",IssuerSigningKeys = discovery.KeySet.Keys /* not quite */
};
JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
SecurityToken validatedToken;
return handler.ValidateToken(jwt,validationParameters,out validatedToken);
}
如何从JsonWebKeySet到IEnumerable< SecurityKey>执行必要的转换?以便验证可以发生?是否有另一种方法(除了OWIN中间件)也可以使用上面的DiscoveryResponse数据? (遗憾的是,System.IdentityModel.Tokens.Jwt的文档不是最新的.) 解决方法检查此示例:https://github.com/IdentityServer/IdentityServer4.Samples/blob/dev/Clients/src/MvcManual/Controllers/HomeController.cs#L81 它从JWK手动检索密钥并填充验证参数. (编辑:日照站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 在ASP.Net中防止SQL注入
- asp.net-mvc – 使用输出缓存和其他动作过滤器
- asp.net-mvc-3 – 在F#中的ViewBag动态对象上设置属性
- asp.net-mvc – Azure git部署 – 第二个程序集中缺少引用
- 将变量从ASP.net传递给JavaScript
- asp.net-mvc – Url.Action如何从模型中添加参数值
- asp.net-mvc – 有条件地在webgrid中显示图像 – mvc 3
- asp.net-mvc-3 – 在ASP.NET MVC 3中覆盖/禁用授权
- asp.net-mvc – mvc razor @helper可以返回非编码标签吗?
- asp.net-mvc – 在MVC4中绑定的正确方法
