gpt4 book ai didi

c# - 验证 JWT 出现奇怪的 “Unable to match key kid” 错误

转载 作者:行者123 更新时间:2023-12-04 13:36:17 27 4
gpt4 key购买 nike

我正在尝试使用下面的代码验证有效的 JWT,但遇到一个奇怪的错误

"IDX10501: Signature validation failed. Unable to match key: 
kid: 'System.String'.
Exceptions caught:
'System.Text.StringBuilder'.
token: 'System.IdentityModel.Tokens.Jwt.JwtSecurityToken'."
这是我的验证方法
 ClaimsPrincipal principal = null;
var token = "JWT GOES HERE"
try
{
string sec = "000uVmTXj5EzRjlnqruWF78JQZMT";
var securityKey = new SymmetricSecurityKey(System.Text.Encoding.Default.GetBytes(sec));

var now = DateTime.UtcNow;
SecurityToken securityToken;

string tokenIssuer = "https://MyIssuer.com";

TokenValidationParameters validationParameters = new TokenValidationParameters()
{
ValidIssuer = tokenIssuer,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
IssuerSigningKey = securityKey
};
JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
principal = handler.ValidateToken(token, validationParameters, out securityToken); <---Errors here
}
这是我的 JWT 的值(value)。我正在使用正确的发行人
{
"alg": "RS256",
"kid": "dev",
"x5t": "Sm7aAUSt4Fdv7X1b9jQDf8XwbvQ",
"pi.atm": "xxe8"
}.{
"scope": [],
"client_id": "ClientABC",
"iss": "https://MyIssuer.com",
"jti": "1JLDz",
"sub": "ClientABC",
"exp": 1601609852
}.[Signature]
我在这里缺少什么? SymmetricSecurityKey 是因为这个算法是 RS256 吗?我在 TokenValidationParameter 中遗漏了什么吗?
更新
经过进一步调查,我得到了错误。
IDX10501: Signature validation failed. Unable to match key: 
kid: 'dev'.
Exceptions caught:
'System.NotSupportedException: IDX10634: Unable to create the SignatureProvider.
Algorithm: 'RS256', SecurityKey: 'Microsoft.IdentityModel.Tokens.SymmetricSecurityKey, KeyId: '', InternalId: 'TdfWgWjCVeM60F3C5TOogJuka1aR5FA_xchwhY9MHH4'.'
is not supported. The list of supported algorithms is available here: https://aka.ms/IdentityModel/supported-algorithms
at Microsoft.IdentityModel.Tokens.CryptoProviderFactory.CreateSignatureProvider(SecurityKey key, String algorithm, Boolean willCreateSignatures, Boolean cacheProvider)

最佳答案

尝试使用 SecurityAlgorithms.HmacSha256
发出 token 时的示例:

Users user = _context.Users.FirstOrDefault(c => c.UserName == userName && c.Password == password); 
if(user == null)
{
return Unauthorized();
}

Claim[] claims = new Claim[]
{
new Claim("Id", user.Id.ToString()),
new Claim("Name", user.Name),
new Claim("Email", user.Email),
};

var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("000uVmTXj5EzRjlnqruWF78JQZMT"));

var signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

var token = new
JwtSecurityToken(
"MyProject",
"MyClient",
claims,
expires: DateTime.Now.AddMinutes(30),
signingCredentials: signingCredentials);

return Ok(new JwtSecurityTokenHandler().WriteToken(token));

如果您使用的是 .net core 应用程序,则在 Startup.cs , 在 配置服务 方法编写此代码以验证 token :
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidAudience = "MyClient",
ValidIssuer = "MyProject",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("000uVmTXj5EzRjlnqruWF78JQZMT"))
};
});

另外不要忘记将以下行添加到 配置 中的方法Startup.cs
app.UseAuthentication();
app.UseAuthorization();

关于c# - 验证 JWT 出现奇怪的 “Unable to match key kid” 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61761730/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com