发生此问题的原因是应用程序未正确配置令牌签名密钥,导致身份验证失败。您可以通过以下步骤解决此问题:
Startup.cs
文件中,打开ConfigureServices
方法。string key = Configuration.GetValue("JwtTokenKey");
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = "yourdomain.com",
ValidAudience = "yourdomain.com",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key))
};
});
appsettings.json
文件中添加以下代码段来存储令牌签名密钥:{
"JwtTokenKey": "MySuperSecretKey1234"
}
这应该解决问题并允许应用程序正确验证JWT令牌。