【发布时间】:2015-12-03 00:31:26
【问题描述】:
我正在尝试实现 JWT 令牌,但一直遇到以下异常:IDX10640: Algorithm is not supported: 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256' when trying to write the token to compact json string.
const string issuer = "issuer";
const string audience = "audience";
byte[] keyForHmacSha256 = new byte[32];
new Random().NextBytes(keyForHmacSha256);
var claims = new List<Claim> { new Claim("deviceId", "12") };
var now = DateTime.UtcNow;
var expires = now.AddHours(1);
var signingCredentials = new SigningCredentials(
new SymmetricSecurityKey(keyForHmacSha256),
SecurityAlgorithms.HmacSha256Signature, SecurityAlgorithms.Sha256Digest);
var token = new JwtSecurityToken(issuer, audience, claims, now, expires, signingCredentials);
return _tokenHandler.WriteToken(token);
有解决这个问题的想法吗?
更新 1:
上述错误发生在 "System.IdentityModel.Tokens.Jwt": "5.0.0-beta7-208241120"
更新 2:
更新代码
【问题讨论】:
-
我遇到了同样的问题。您使用的是
5.0.0-beta7-208241120版本的System.IdentityModel.Tokens.Jwt库吗? -
是的,很高兴听到我不是唯一一个......
-
1) 为什么要使用
System.Random创建加密密钥? 2) 128 字节的密钥没有意义。您想要一个 128 位密钥(16 字节)吗? 256 位/32 字节也是一个明智的选择。 3) 使用当地时间也很奇怪。 -
1) 我写这只是为了简化代码部分 2) 128/256 位密钥也会发生错误 3) 已修复
-
见下面布伦特的回答和这张票的状态:github.com/AzureAD/…