我有一个公开 API 端点的 ASP.NET 核心 2.2 应用程序。该端点受一个简单的 JWT 令牌保护,该令牌只关心主题、过期和签名(使用共享密钥的 SHA256)。
当我从 .net 核心客户端调用端点时,它工作正常。
我现在正试图从 Delphi 客户端调用相同的代码,但服务器拒绝这些令牌,我不知道为什么:两个令牌看起来与我相同,它们都通过了 jwt.io 的验证测试
服务器端令牌验证码:
private static TokenValidationParameters GetValidationParameters(byte[] key)
{
return new TokenValidationParameters()
{
ValidateLifetime = true,
LifetimeValidator = (DateTime? notBefore, DateTime? expires, SecurityToken securityToken, TokenValidationParameters validationParameters) =>
{
// Cutom validity validation
DateTime current = DateTime.UtcNow; // get a single value for "now"
// check if the token expiration is valid and if it doesn't expire
bool value = (expires.HasValue) && (notBefore.HasValue) && // both "expires" and "notBefore" must be set
(expires >= current) && // "expires" must not be in the past
(expires <= current.AddMinutes(5)) && // "expires" must not be any further than 5 minutes in the future
(notBefore <= current); // notBefore must be in the past or present
return value;
},
RequireExpirationTime = true,
// the token has no audience or issuer so ignore these
ValidateAudience = false,
ValidateIssuer = false,
IssuerSigningKey = new SymmetricSecurityKey(key)
};
}
private bool ValidateToken(string authToken, byte[] key)
{
var tokenHandler = new JwtSecurityTokenHandler();
var validationParameters = GetValidationParameters(key);
// This will raise an exception if the security token is invalid
try
{
tokenHandler.ValidateToken(authToken, validationParameters, out SecurityToken validatedToken);
}
catch (SecurityTokenException e) // this will happen if the token is properly formated but invalid (signature, validity)
{
logger.LogInformation("Invalid token received: {1}", e.Message);
return false;
}
return true;
}
错误发生在tokenHandler.ValidateToken
通话中。令牌过期验证码永远不会被调用。
样本令牌
从 C# 应用程序(通过验证):
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NjM2MDQ1My1CMDJCLUU5MTEtODM5Qy1BMEE4Q0QzQUNCRjgiLCJuYmYiOjE1NTg2MTMzOTQsImV4cCI6MTU1ODYxMzY5NCwiaWF0IjoxNTU4NjEzMzk0fQ.nso4xnllNc-rXfn5riOWv5fZjNeJMgoQbyXeOltDYb0
从我的 Delphi 应用程序(失败):
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI2NjM2MDQ1My1CMDJCLUU5MTEtODM5Qy1BMEE4Q0QzQUNCRjgiLCJpYXQiOjE1NTg2MTYxNTgsIm5iZiI6MTU1ODYxNjE1OCwiZXhwIjoxNTU4NjE2NDU4fQ.vB_gotDk1JGiiDWPT0t6TR471Av4r-LXSgc3zab7EaU
报告的错误:
IDX10503: Signature validation failed. Keys tried: 'Microsoft.IdentityModel.Tokens.SymmetricSecurityKey , KeyId:
'.
Exceptions caught:
''.
token: '{"typ":"JWT","alg":"HS256"}.{"sub":"66360453-B02B-E911-839C-A0A8CD3ACBF8","iat":1558616158,"nbf":1558616158,"exp":1558616458}'.
两个令牌都使用相同的共享密钥(b64 编码)进行保护:
NdFCOQReqUk0mxTqI7psd9JrVjgE7bdPVfjILEa4dzE=
(所有这些数据都来自本地测试应用程序,因此产生这个问题没有任何秘密)