我正在尝试实施JWT令牌,但仍然遇到以下异常:IDX10640:不支持算法:' http://www.w3.org/2001/04/xmldsig-more#hmac-sha256'在尝试将令牌写入压缩json字符串时。
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:
以上错误发生在&#34; System.IdentityModel.Tokens.Jwt&#34;:&#34; 5.0.0-beta7-208241120&#34;
更新2:
更新了代码
答案 0 :(得分:3)
支持将在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);
答案 1 :(得分:2)
我们目前不支持对称密钥。希望尽快得到它。