【问题标题】:DNX Core 5.0 JwtSecurityTokenHandler "IDX10640: Algorithm is not supported: 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256'"DNX Core 5.0 JwtSecurityTokenHandler “IDX10640:不支持算法:'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256'”
【发布时间】: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/…

标签: c# json jwt adal


【解决方案1】:

我们目前不支持对称密钥。希望能尽快实现。

【讨论】:

    【解决方案2】:

    支持将在 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);
    

    【讨论】:

      猜你喜欢
      • 2021-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      相关资源
      最近更新 更多