概述
探讨 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.JwtBearer 6.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
来源链接:https://www.cnblogs.com/jnzhsh/p/18763122
没有回复内容