How to replace AddJwtBearer extension in .NET Core 3.0

27,341

Solution 1

Like Mert Sayin says, include package Microsoft.AspNetCore.Authentication.JwtBearer, but use Version 3.0.0.

Solution 2

You must include Microsoft.AspNetCore.Authentication.JwtBearer package to your project. with version 3.0.0 for Core 3.0 and above.

Solution 3

Something like this:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => Configuration.Bind("JwtSettings", options)) .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options => Configuration.Bind("CookieSettings", options));

From here: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/?view=aspnetcore-3.1

Share:
27,341
shelbypereira
Author by

shelbypereira

.NET Software Architect with 10 years experience. considerable experience with embedded C++ on the Linux platform and some Java development. Experience with all the major DBs (Oracle, SQL Server, MySQL) currently mostly working with .NET Core 3.0 on medical applications including Dicom imaging software.

Updated on November 02, 2020

Comments

  • shelbypereira
    shelbypereira over 3 years

    I have the following code which compiles and works in .NET Core 2.2:

      byte[] key = Encoding.ASCII.GetBytes(Constants.JWT_SECRET); 
            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata = false;
                x.SaveToken = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(key),
                    ValidateIssuer = false,
                    ValidateAudience = false
                };
            });
    

    In .NET Core 3.0 I am getting the error:

    Error CS1061 'AuthenticationBuilder' does not contain a definition for 'AddJwtBearer' and no accessible extension method 'AddJwtBearer' accepting a first argument of type 'AuthenticationBuilder' could be found (are you missing a using directive or an assembly reference?)

    when I look at the MSFT documentation: https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.jwtbearerextensions.addjwtbearer?view=aspnetcore-2.2

    and try to got to version 3.0, It seems that this is the last version where this is defined. How do I migrate AddJwtBearer to Core 3.0?

  • Mert Sayın
    Mert Sayın over 4 years
    Did you use using Microsoft.AspNetCore.Authentication.JwtBearer; in top?
  • shelbypereira
    shelbypereira over 4 years
    yes I do, I don't think it is so simple since the MSFT documentation clearly states that this method does not exist in 3.0. I think there may be some way to use Dependency Injection in startup to add the JwtBearer but I haven't been able to find on internet.