我正在使用IdentityServer4来保护多个服务的原型,但需要注意的是,这些服务很可能不会被迁移(在可预见的未来)以使用ASP.NET Core的OWIN中间件习惯用法.因此,我不能利用许多中间件助手来自动验证JWT,只需提供IdentityServer的着名JWKS端点等等.
如果我能重建这种行为会很好,我想尽可能利用微软的JwtSecurityTokenHandler
实现.但是,我无法弄清楚如何利用通过IdentityServer的发现端点提供的JsonWebKeySet和JsonWebKey类型来提取密钥并执行验证.
JwtSecurityTokenHandler使用TokenValidationParameters
来验证JWT,这些参数需要一个或多个SecurityKey
对象的实例来执行验证.
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的文档不是最新的.)
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。