如下代码:

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的“标题”部分为:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6MH0


可以将其解码为:

{"alg":"HS256","typ":"JWT","kid":0}


错误令牌的来源是Azure Mobile Services

请注意,当“标头”部分为以下内容时,在调用同一行代码时不会发生该异常:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjAifQ


可以将其解码为:

{"alg":"HS256","typ":"JWT","kid":"0"}


如何克服这个问题,并正确验证这样的令牌?

最佳答案

就其与JWT规范的一致性而言,这是Azure移动服务中的一个错误。此错误已得到修复,因此由移动服务生成的JWT现在应该在其标头的“ kid”字段中具有正确的字符串值。

关于c# - 解码Azure移动服务JWT token 时出现JwtSecurityToken异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18273776/

10-12 01:32