我在验证JWT签名时遇到一个非常奇怪的错误,这在本地运行Web应用程序时工作正常但是当部署到服务器时我收到此错误:
“消息:IDX10503:签名验证失败。密钥尝试:'System.IdentityModel.Tokens.InMemorySymmetricSecurityKey \ r \ n'。\ n捕获的异常:\ n''。\ ntoken:'{\”typ \“:\ “JWT \”,\ “ALG \”:\ “HS256 \”} .....
我像这样创建我的令牌
var securityKey = Encoding.UTF8.GetBytes(secret);
var tokenHandler = new JwtSecurityTokenHandler();
// Token Creation
var now = DateTime.UtcNow;
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = identity,
TokenIssuerName = "self",
AppliesToAddress = "http://example.com",
Lifetime = new Lifetime(now, now.AddMinutes(2)),
SigningCredentials = new SigningCredentials(
new InMemorySymmetricSecurityKey(securityKey),
"http://www.w3.org/2001/04/xmldsig-more#hmac-sha256",
"http://www.w3.org/2001/04/xmlenc#sha256"),
};
var token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token);
并验证如下:
// Create token validation parameters for the signed JWT
// This object will be used to verify the cryptographic signature of the received JWT
TokenValidationParameters validationParams =
new TokenValidationParameters()
{
ValidAudience = "http://example.com",
ValidIssuer = "self",
IssuerSigningToken = new BinarySecretSecurityToken(Encoding.UTF8.GetBytes(secret)),
};
var tokenHandler = new JwtSecurityTokenHandler();
// tokenHandler.ValidateToken(token, validationParams);
SecurityToken st;
principal = tokenHandler.ValidateToken(token, validationParams, out st);
return true;
调用“token.ValidateToken(..)”
时发生错误有什么想法吗?