Missing AddJwtBearerAuthentication () extension method for IServiceCollection in .NET Core 2.0

I upgraded my project from Core 1.1 to Core 2.0 using the instructions https://blogs.msdn.microsoft.com/webdev/2017/08/14/announcing-asp-net-core-2-0/ (updated target environment for .NET Core 2.0 and meta-package Microsoft.AspNetCore.All). I also updated all possible nuget packages to the latest versions.

In .NET Core 1.1, I added JWT media authentication:

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

According to http://www.talkingdotnet.com/whats-new-in-asp-net-core-2-0/ for Core 2.0, a new way:

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

But the AddJwtBearerAuthentication () method is missing. Installed package Microsoft.AspNetCore.Authentication.JwtBearer 2.0.0.

New empty Core 2.0 projects (with the JwtBearer package) also do not have a method to add AddJwtBearerAuthentication () for IServiceCollection.

The old app.UseJwtBearerAuthentication () method does not compile at all:

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

Please, help.

+5
source share
3 answers

In ConfigureServices, use the following code to configure JWTBearer authentication:

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

And in Configure before UseMvc() add UseAuthentication() :

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

Detailed example: https://github.com/aspnet/Security/blob/dev/samples/JwtBearerSample/Startup.cs#L51

+7
source

Method that configures Jwt authentication:

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

Now, using the ConfigureServices () method for Startup.cs, you can call the ConfigureJwtAuthService () method to configure Jwt authentication.

+2
source
 services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.Audience = "http://localhost:5001/"; options.Authority = "http://localhost:5000/"; }); 

see https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x

0
source

Source: https://habr.com/ru/post/1270917/


All Articles