我試圖在ASP.NET Core Web API中實現JWT。在JwtBearer的版本1.1.2我有這樣的代碼在我Startup.cs文件:ASP.NET核心 - JwtBearer 2.0.0
public void ConfigureServices(IServiceCollection services)
{
...
services.Configure<JwtSettings>(Configuration.GetSection("jwt"));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
var jwtSettings = app.ApplicationServices.GetService<JwtSettings>();
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = jwtSettings.Issuer; // "http://localhost:5000"
ValidateAudience = false,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.Key)) // some secret Key
}
});
app.UseMvc();
}
如何這個代碼應該看起來JwtBearer 2.0.0?
我安裝了這個包是這樣的:
dotnet的附加包Microsoft.AspNetCore.Authentication.JwtBearer
我的.NET Core版本:
2.0.0