ASP.NET核心 - JwtBearer 2.0.0

时间:2017-09-06 11:29:37

标签: c# asp.net jwt

我试图在ASP.NET Core Web API中实现JWT。在JwtBearer的1.1.2版中,我在Startup.cs文件中有这个代码:

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 add package Microsoft.AspNetCore.Authentication.JwtBearer

我的.NET核心版本:

  

2.0.0

2 个答案:

答案 0 :(得分:1)

基于以上代码将此转换为dotnet core 2.0,您需要重构如下...根据您的要求进行调整,这是在传输过程中的快速答案。

public void ConfigureServices(IServiceCollection services)
{
    // ...
    services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        }).AddJwtBearer(o => {
            o.Audience = Configuration.GetSection("jwt:Audience").Value;
            o.Authority = Configuration.GetSection("jwt:Authority").Value;
            o.RequireHttpsMetadata = Configuration.GetValue<bool>("jwt:RequireHttps");
            o.Events = new JwtBearerEvents()
            {
                OnAuthenticationFailed = c =>
                {
                    c.NoResult();
                    c.Response.StatusCode = 401;
                    return c.Response.WriteAsync("Invalid Token");
                }
            };
        });
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        // ...
        app.UseAuthentication();
        // ...
        app.UseMvc();
    }

答案 1 :(得分:1)

这可能对您有用:

public void ConfigureServices(IServiceCollection services)

{

      services.Configure<JwtSettings>(Configuration.GetSection("jwt"));

      var provider = services.BuildServiceProvider();     

      var jwtSettings = provider.GetService<JwtSettings>();


        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(o =>
            {                                   
                o.TokenValidationParameters = new TokenValidationParameters
                {                        
                    ValidateIssuer = jwtSettings.Issuer;
                    ValidateAudience = false,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.Key)) // some secret Key

                };                   

            });




}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
 ....

  app.UseAuthentication();
  app.UseMvc();

}