5

J'ai mis à jour mon projet de Core 1.1 Core 2.0 en suivant les instructions de https://blogs.msdn.microsoft.com/webdev/2017/08/14/announcing-asp-net-core-2-0/ (cadre cible mis à jour .NET Core 2.0 et utilisé métapaquet Microsoft.AspNetCore.All) . J'ai également mis à jour tous les paquets de nuget possibles vers les dernières versions.méthode d'extension manquant AddJwtBearerAuthentication() pour IServiceCollection dans .NET Core 2.0

Dans .NET Core 1.1 i ajoutais JWT Bearer authentification de cette façon:

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

Comme par http://www.talkingdotnet.com/whats-new-in-asp-net-core-2-0/ Core 2.0 pour la nouvelle façon est d'appeler:

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

Mais la méthode AddJwtBearerAuthentication() est absent. Le package Microsoft.AspNetCore.Authentication.JwtBearer 2.0.0 est installé.

De nouveaux projets Core 2.0 vides (avec le package JwtBearer) n'ont pas non plus la méthode d'extension AddJwtBearerAuthentication() pour IServiceCollection.

L'ancienne méthode app.UseJwtBearerAuthentication() ne compile pas du tout:

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

S'il vous plaît aider.

Répondre

7

En ConfigureServices utiliser le code suivant pour configurer JWTBearer authentification:

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

Et Configure juste avant UseMvc() ajouter UseAuthentication():

app.UseAuthentication(); 

app.UseStaticFiles(); 

app.UseMvc(); 

Pour un exemple détaillé voir: https://github.com/aspnet/Security/blob/dev/samples/JwtBearerSample/Startup.cs#L51

0

méthode configurer l'authentification Jwt:

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

maintenant à l'intérieur des ConfigureServices() méthode du Startup.cs, vous pouvez appeler ConfigureJwtAuthService() méthode pour configurer le Jwt authentification.