DNX 核心 5.0 JwtSecurityTokenHandler "IDX10640: Algorithm is not supported: 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256'"

DNX Core 5.0 JwtSecurityTokenHandler "IDX10640: Algorithm is not supported: 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256'"

我正在尝试实现 JWT 令牌,但将 运行 保留在以下异常中:IDX10640:不支持算法:尝试将令牌写入压缩 [=30 时,'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256' =] 字符串.

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"

更新二:

更新代码

我们目前不支持对称密钥。希望尽快得到它。

将在 RC2 版本中提供支持。 使用来自 http://myget.org/gallery/azureadwebstacknightly

的夜间 nuget 包进行测试

只需稍作代码更改即可使一切正常工作

const string issuer = "issuer";
const string audience = "audience";
var keyForHmacSha256 = Encoding.ASCII.GetBytes("<tokenSecret>");
var key = new SymmetricSecurityKey(keyForHmacSha256);
var claims = new List<Claim> { new Claim("deviceId", "12") };
var now = DateTime.UtcNow;
var expires = now.AddHours(1);
var signingCredentials = new SigningCredentials(key, SecurityAlgorithms.HMAC_SHA256);

var token = new JwtSecurityToken(issuer, audience, claims, now, expires, signingCredentials);
return _tokenHandler.WriteToken(token);

可以使用下一段代码验证令牌

SecurityToken securityToken;
var validationParameters = new TokenValidationParameters
{
    ValidateLifetime = true,
    ValidateAudience = true,
    ValidateIssuer = true,
    RequireExpirationTime = true,
    ValidateSignature = true,
    ValidAudience = audience,
    ValidIssuer = issuer,
    IssuerSigningKey = key,
    RequireSignedTokens = true,
    ValidateIssuerSigningKey = true               
};

tokenHandler.ValidateToken(token, validationParameters, out securityToken);