【问题标题】:ASP.NET Core - JwtBearer 2.0.0ASP.NET Core - JwtBearer 2.0.0
【发布时间】:2017-09-14 13:00:47
【问题描述】:

我正在尝试在 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 添加包 Microsoft.AspNetCore.Authentication.JwtBearer

我的 .NET Core 版本:

2.0.0

【问题讨论】:

标签: c# asp.net jwt


【解决方案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();
    }

【讨论】:

    【解决方案2】:

    这可能对你有用:

    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();
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-27
      • 2021-11-01
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 2017-10-30
      • 2018-02-04
      相关资源
      最近更新 更多