考虑到在外部创建JWT时无法影响JWT,当令牌不包含kid
时,我如何验证它的签名。
以下是有关守则:
private bool ValidateToken(string authToken)
{
var tokenHandler = new JwtSecurityTokenHandler();
var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(_secretKey));
var validationParameters = new TokenValidationParameters()
{
ValidAudience = "clientid",
ValidIssuer = _issuer,
ValidateAudience = false,
ValidateIssuer = false,
IssuerSigningKey = new SymmetricSecurityKey(hmac.Key),
};
try
{
tokenHandler.ValidateToken(authToken, validationParameters, out SecurityToken validatedToken);
}
catch (Exception ex)
{
//handle exception
}
return true;
}
但是,这会引发异常:
{
"IDX10503: Signature validation failed.
Token does not have a kid.
Keys tried: '[PII of type 'System.Text.StringBuilder' is hidden.
For more details, see https://aka.ms/IdentityModel/PII.]'.
Number of keys in TokenValidationParameters: '1'.
Number of keys in Configuration: '0'.
Exceptions caught:
'[PII of type 'System.Text.StringBuilder' is hidden.
For more details, see https://aka.ms/IdentityModel/PII.]'.
token: '[PII of type 'System.IdentityModel.Tokens.Jwt.JwtSecurityToken' is hidden.
For more details, see https://aka.ms/IdentityModel/PII.]'."
}
为什么微软坚持在令牌中有一个关键标识?有没有办法忽略丢失的kid
?
示例令牌(编辑以删除数据):
标题:
{
"alg": "HS256",
"typ": "JWT",
"ver": 1,
"typ_2": "ref"
}
有效载荷:
{
"jti": "<token_id>",
"client_id": "<client_id>",
"client_name": "<client>",
"ref_token": "<ref_token>",
"ref_token_type": "Full",
"zone": "<zone>",
"endusertype": "system",
"nbf": 1643639617,
"exp": 1643643217,
"iat": 1643639617,
"iss": "<issuer>"
}
编辑:删除IssuerSigningKeys以避免混淆。
发布于 2022-02-03 11:05:55
在验证编码和抓取我的头相当一段时间之后,我设法找到了外部方,他们确认密钥确实是错误的-- " key“只打算在api中使用--在调用中,使用者请求一个令牌,以”验证“使用者。他们没有在jwt中包含一个kid
,因为他们不打算让消费者验证令牌。
然而,这并没有回答这个问题。
https://stackoverflow.com/questions/70941707
复制相似问题