我是JWT的新手,我试图在我的一个项目中实现它。在阅读有关JWT的一些文章时,我遇到了一个名为TokenValidationParameters的类,该类几乎没有在实现JWT时必须初始化的属性。我想详细了解这些属性的用途,为什么要使用它们,希望举个例子
public class Startup
{
public void ConfigureServices (IServiceCollection services)
{
services.AddAuthentication (JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer (options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Jwt:Issuer"],
ValidAudience = Configuration["Jwt:Issuer"],
IssuerSigningKey = new
SymmetricSecurityKey (Encoding.UTF8.GetBytes (Configuration["Jwt:Key"]))
};
});
services.AddMvc ();
}
}
appsettings.json:
{
"Jwt": {
"Key": "SecretKey",
"Issuer": "http://localhost:64161/"
}
}