找回密码
 立即注册
首页 业界区 安全 asp net core下 JWT 部分 密钥生成错误(IDX10720)和 C ...

asp net core下 JWT 部分 密钥生成错误(IDX10720)和 Claim 类型映射问题

蒋炸役 2025-5-30 10:05:13
概述

探讨 JWT 签发与验证中的常见问题:密钥生成错误(IDX10720)和 Claim 类型映射问题,提供解决方案与代码示例,助你轻松避坑。
踩坑详情

坑一: IDX10720 错误 - 无法为算法 'HS256' 创建 KeyedHashAlgorithm

问题描述

在项目中引入了Microsoft.AspNetCore.Authentication.JwtBearer (6.0.26) 由 6.0.25 升级,之前用于签发 JWT Token 的代码报错:IDX10720: Unable to create KeyedHashAlgorithm for algorithm 'HS256'"
问题代码示例
  1. var credentials=GenerateCredentials(_options.Key);
  2. var header = new JwtHeader(credentials);
  3. var payload = new JwtPayload(_options.Issuer,_options.Audience,claim,notBefore,expoires);
  4. var token = new JwtSecurityToken(header, payload);
  5. var tokenHandler = new JwtSecurityTokenHandler();
  6. var jwtToken = tokenHandler.WriteToken(token);
  7. SigningCredentials GenerateCredentials(string key)
  8. {
  9.      return new SigningCredentials(GenerateKey(key), SecurityAlgorithms.HmacSha256);
  10. }
  11. SecurityKey GenerateKey(string key)
  12. {
  13.     return new SymmetricSecurityKey(MD5.HashData(Encoding.UTF8.GetBytes(str)));
  14. }
复制代码
问题原因

Microsoft.AspNetCore.Authentication.JwtBearer(6.0.26) 版本依赖的下游包 Microsoft.IdentityModel.Protocols.OpenIdConnect(6.35.0) 对密钥的生成方式进行了更严格的校验,要求密钥必须满足特定算法的位数要求。
使用 MD5 生成的密钥长度不符合 HS256 算法的要求。
具体下游包升级详情
  1. Microsoft.AspNetCore.Authentication.JwtBearer  6.0.25 -> 6.0.26
  2.     Microsoft.IdentityModel.Protocols.OpenIdConnect 6.10.0 -> 6.35.0
复制代码
解决方案

根据项目情况,选择以下方案之一:

  • 降Microsoft.AspNetCore.Authentication.JwtBearer至6.0.25
  • 使用SHA256 生成密钥,满足位数要求SHA256.HashData()
相关资料

https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/pull/2072
https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.JwtBearer/6.0.25#dependencies-body-tab
https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.JwtBearer/6.0.26#dependencies-body-tab
坑二: JwtRegisteredClaimNames.Sub类型映射问题

问题描述

在签发 JWT Token 时使用了特定的 Claim 类型(如 JwtRegisteredClaimNames.Sub),但在使用 HttpContext.User.Claims 读取时无法正确获取。
问题代码示例
  1. var claim = new Claim(JwtRegisteredClaimNames.Sub,Id);
  2. // cant find
  3. var claim = Httpcontext.User.Claims.FirstOrDefault(x=>x.Key == JwtRegisteredClaimNames.Sub);
  4. // you shold use like this to find
  5. var claim = Httpcontext.User.Claims.FirstOrDefault(x=>x.Key == ClaimTypes.NameIdentifier);
复制代码
问题原因

JwtSecurityTokenHandler 默认会将一些标准的 JWT Claim 类型映射到 .NET 的 ClaimTypes。例如,JwtRegisteredClaimNames.Sub 会被映射为 ClaimTypes.NameIdentifier。
解决方案

根据项目情况,选择以下方案之一:

  • 将 MapInboundClaims  设置成false
  1. JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
  2. handler.MapInboundClaims = false;
  3. //or
  4. JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear()
复制代码

  • 使用自定义字段 映射
  1. var claim = new Claim("your_type",Id);
  2. var claim = Httpcontext.User.Claims.FirstOrDefault(x=>x.Key == "your_type");
复制代码

  • 使用 ClaimTypes.NameIdentifier来获取被Mapping后的值
  1. var claim = Httpcontext.User.Claims.FirstOrDefault(x=>x.Key == ClaimTypes.NameIdentifier);
复制代码
相关资料

https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/issues/415
https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/rel/6.25.0/src/System.IdentityModel.Tokens.Jwt/ClaimTypeMapping.cs

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册