5

Tôi đã cập nhật dự án của mình từ Core 1.1 lên Core 2.0 bằng cách sử dụng các hướng dẫn từ https://blogs.msdn.microsoft.com/webdev/2017/08/14/announcing-asp-net-core-2-0/ (khung đích được cập nhật lên .NET Core 2.0 và được sử dụng siêu tệp Microsoft.AspNetCore.All) . Tôi đã cập nhật tất cả các gói nuget có thể lên phiên bản mới nhất.Thiếu phương thức mở rộng AddJwtBearerAuthentication() cho IServiceCollection trong .NET Core 2.0

Trong .NET Lõi 1.1 tôi đã thêm JWT Bearer Xác thực theo cách này:

app.UseJwtBearerAuthentication(); // from Startup.Configure() 

Theo http://www.talkingdotnet.com/whats-new-in-asp-net-core-2-0/ cho Core 2.0 cách mới là để gọi:

services.AddJwtBearerAuthentication(); // from Startup.ConfigureServices() 

Nhưng phương pháp AddJwtBearerAuthentication() không có mặt. Gói Microsoft.AspNetCore.Authentication.JwtBearer 2.0.0 được cài đặt.

Dự án Core 2.0 trống mới (với gói JwtBearer) cũng không có phương thức mở rộng AddJwtBearerAuthentication() cho IServiceCollection.

Các cũ phương pháp app.UseJwtBearerAuthentication() không biên dịch tại tất cả:

Error CS0619 'JwtBearerAppBuilderExtensions.UseJwtBearerAuthentication(IApplicationBuilder, JwtBearerOptions)' is obsolete: 'See https://go.microsoft.com/fwlink/?linkid=845470' 

Xin vui lòng giúp.

Trả lời

7

Trong ConfigureServices sử dụng đoạn mã sau để cấu hình JWTBearer Xác thực:

public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddAuthentication(o => 
     { 
      o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; 
      o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; 
     }).AddJwtBearer(o => 
     { 
      o.Authority = "https://localhost:54302"; 
      o.Audience = "your-api-id"; 
      o.RequireHttpsMetadata = false; 
     }); 

     services.AddMvc(); 
    } 

Và trong Configure ngay trước UseMvc() thêm UseAuthentication():

app.UseAuthentication(); 

app.UseStaticFiles(); 

app.UseMvc(); 

Đối với một ví dụ chi tiết xem: https://github.com/aspnet/Security/blob/dev/samples/JwtBearerSample/Startup.cs#L51

0

Phương pháp mà cấu hình JWT xác thực:

// Configure authentication with JWT (Json Web Token). 
public void ConfigureJwtAuthService(IServiceCollection services) 
{ 
    // Enable the use of an [Authorize(AuthenticationSchemes = 
    // JwtBearerDefaults.AuthenticationScheme)] 
    // attribute on methods and classes to protect. 
    services.AddAuthentication().AddJwtBearer(cfg => 
    { 
    cfg.RequireHttpsMetadata = false; 
    cfg.SaveToken = true; 
    cfg.TokenValidationParameters = new TokenValidationParameters() 
    { 
     IssuerSigningKey = JwtController.SecurityKey, 
     ValidAudience = JwtController.Audience, 
     ValidIssuer = JwtController.Issuer, 
     // When receiving a token, check that we've signed it. 
     ValidateIssuerSigningKey = true, 
     // When receiving a token, check that it is still valid. 
     ValidateLifetime = true, 
     // This defines the maximum allowable clock skew when validating 
     // the lifetime. As we're creating the tokens locally and validating 
     // them on the same machines which should have synchronised time, 
     // this can be set to zero. 
     ClockSkew = TimeSpan.FromMinutes(0) 
    }; 
    }); 
} 

Bây giờ bên trong ConfigureServices() phương pháp của Startup.cs, bạn có thể gọi ConfigureJwtAuthService() phương pháp để cấu hình JWT xác thực.

Các vấn đề liên quan