5
Ben https://blogs.msdn.microsoft.com/webdev/2017/08/14/announcing-asp-net-core-2-0/ den Çekirdek yönergeleri kullanarak 2.0'a Çekirdek 1.1 den projemi güncelledik

(NET Çekirdek 2.0 güncellendi hedef çerçevesinde ve kullanılan meta pakettir Microsoft.AspNetCore.All) . Tüm olası nuget paketlerini de en son sürümlere güncelledim. .NET Çekirdek i JWT Taşıyıcı kimlik denetimi bu şekilde ekleyerek oldu 1.1 eksik uzantı yöntemi AddJwtBearerAuthentication() 2.0

:

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

Ama yöntem AddJwtBearerAuthentication:

Çekirdek 2.0 yeni yolu için http://www.talkingdotnet.com/whats-new-in-asp-net-core-2-0/ gereğince
app.UseJwtBearerAuthentication(); // from Startup.Configure() 

çağırmaktır() eksik. paket Microsoft.AspNetCore.Authentication.JwtBearer 2.0.0 yüklü. (JwtBearer paketi ile)

Yeni boş Çekirdek 2.0 projeler IServiceCollection için uzatma yöntemi AddJwtBearerAuthentication() yoktur da bulunmaktadır.

eski yöntem app.UseJwtBearerAuthentication() hiç derleme değil:

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

yardım edin. ConfigureServices ise

cevap

7

JWTBearer Kimlik Doğrulama yapılandırmak için aşağıdaki kodu kullanın:

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

Ve Configure sadece UseMvc() önce UseAuthentication() ekleyin:

app.UseAuthentication(); 

app.UseStaticFiles(); 

app.UseMvc(); 

Ayrıntılı bir örnek için bkz: https://github.com/aspnet/Security/blob/dev/samples/JwtBearerSample/Startup.cs#L51

0

Yöntem bkz: ConfigureServices içeride Şimdi

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

() Startup.cs ait yöntemi, sen JWT yapılandırmak için ConfigureJwtAuthService() yöntemini çağırabilirsiniz kimlik doğrulaması.

İlgili konular