蒋炸役 发表于 2025-5-30 10:05:13

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

概述

探讨 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'"
问题代码示例

var credentials=GenerateCredentials(_options.Key);
var header = new JwtHeader(credentials);
var payload = new JwtPayload(_options.Issuer,_options.Audience,claim,notBefore,expoires);
var token = new JwtSecurityToken(header, payload);
var tokenHandler = new JwtSecurityTokenHandler();
var jwtToken = tokenHandler.WriteToken(token);


SigningCredentials GenerateCredentials(string key)
{
   return new SigningCredentials(GenerateKey(key), SecurityAlgorithms.HmacSha256);
}

SecurityKey GenerateKey(string key)
{
    return new SymmetricSecurityKey(MD5.HashData(Encoding.UTF8.GetBytes(str)));
}问题原因

Microsoft.AspNetCore.Authentication.JwtBearer(6.0.26) 版本依赖的下游包 Microsoft.IdentityModel.Protocols.OpenIdConnect(6.35.0) 对密钥的生成方式进行了更严格的校验,要求密钥必须满足特定算法的位数要求。
使用 MD5 生成的密钥长度不符合 HS256 算法的要求。
具体下游包升级详情
Microsoft.AspNetCore.Authentication.JwtBearer6.0.25 -> 6.0.26
    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 读取时无法正确获取。
问题代码示例

var claim = new Claim(JwtRegisteredClaimNames.Sub,Id);

// cant find
var claim = Httpcontext.User.Claims.FirstOrDefault(x=>x.Key == JwtRegisteredClaimNames.Sub);

// you shold use like this to find
var claim = Httpcontext.User.Claims.FirstOrDefault(x=>x.Key == ClaimTypes.NameIdentifier);问题原因

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

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

[*]将 MapInboundClaims设置成false
JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
handler.MapInboundClaims = false;
//or
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear()
[*]使用自定义字段 映射
var claim = new Claim("your_type",Id);
var claim = Httpcontext.User.Claims.FirstOrDefault(x=>x.Key == "your_type");
[*]使用 ClaimTypes.NameIdentifier来获取被Mapping后的值
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

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

锟及 发表于 2025-10-10 22:32:18

前排留名,哈哈哈

溶绚 发表于 2025-10-13 00:41:52

不错,里面软件多更新就更好了

费卿月 发表于 2025-10-24 05:36:05

鼓励转贴优秀软件安全工具和文档!

损注 发表于 2025-11-28 00:55:41

喜欢鼓捣这些软件,现在用得少,谢谢分享!

趣侮 发表于 2025-12-11 15:29:16

喜欢鼓捣这些软件,现在用得少,谢谢分享!

崆蛾寺 发表于 2025-12-21 15:57:16

很好很强大我过来先占个楼 待编辑

稞冀 发表于 2025-12-21 17:25:01

鼓励转贴优秀软件安全工具和文档!

岭猿 发表于 2025-12-21 21:29:45

这个好,看起来很实用

掳诚 发表于 2025-12-24 17:08:30

这个有用。

热琢 发表于 2026-1-6 14:16:45

不错,里面软件多更新就更好了

驶桐柢 发表于 2026-1-7 18:45:43

鼓励转贴优秀软件安全工具和文档!

愆蟠唉 发表于 2026-1-14 07:33:31

分享、互助 让互联网精神温暖你我

少琼 发表于 2026-1-14 20:04:56

很好很强大我过来先占个楼 待编辑

届表 发表于 2026-1-19 10:28:54

收藏一下   不知道什么时候能用到

裴竹悦 发表于 2026-1-21 11:08:20

谢谢楼主提供!

赖琳芳 发表于 2026-1-22 03:42:58

收藏一下   不知道什么时候能用到

伯斌 发表于 2026-1-22 04:23:57

感谢分享,学习下。

梢疠 发表于 2026-1-23 07:19:24

感谢,下载保存了

琉艺戕 发表于 2026-2-4 08:28:57

很好很强大我过来先占个楼 待编辑
页: [1] 2
查看完整版本: asp net core下 JWT 部分 密钥生成错误(IDX10720)和 Claim 类型映射问题