问题描述
以下代码:
using System.IdentityModel.Tokens;
JwtSecurityToken jwtSecurityToken = new JwtSecurityToken(rawToken);
产生以下异常:
Jwt10113: Unable to decode the 'header'
The value "0" is not of type "System.String" and cannot be used in this generic collection. Parameter name: value
当 rawToken 的 'header' 部分是:
When the 'header' section of the rawToken is:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6MH0
可以解码为:
{"alg":"HS256","typ":"JWT","kid":0}
错误令牌的来源是 Azure 移动服务
.
The source of the faulting token is Azure Mobile Services
.
请注意,当'header'部分是:调用同一行代码时不会发生异常:
Please note that the exception does not occur when calling the same line of code while the 'header' section is:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjAifQ
可以解码为:
{"alg":"HS256","typ":"JWT","kid":"0"}
我怎样才能克服这个问题,并正确验证这样的令牌?
How can I overcome this problem, and properly validate such a token?
推荐答案
这是 Azure 移动服务中的一个错误,它与 JWT 规范的一致性有关.该错误已得到修复,因此移动服务生成的 JWT 现在应该在其标头的kid"字段中具有正确的字符串值.
This was a bug in the Azure Mobile Services with respect to its conformance with the JWT spec. The bug has since been fixed, so the JWTs generated by the mobile services should now have a correct string value in the "kid" field of its header.
这篇关于解码 Azure 移动服务 JWT 令牌时出现 JwtSecurityToken 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!