Migrating JWT from ASP.NET Core 1.x to ASP.NET Core 2.0

Asp.Net Core 2.0 inroduces some breaking changes :  https://blogs.msdn.microsoft.com/webdev/2017/08/14/announcing-asp-net-core-2-0/

In this tutorial, I will upgrade an asp.core 1.1 to asp.net core 2.0 using JWT based authentification and Xunit  with sqlLite and also with LocalDB

To migrate JW from asp.net core 1.x to asp.net core 2.0 proceed as follows

1. Go to project properties and choose .NET Core 2.0 as TargetFramework

TargetFramework

2. open ConfigureServices of startup.cs file and paste this line of code :

   services.AddIdentity<MyUser, MyRole>().AddEntityFrameworkStores<SecurityContext>();
        services.AddAuthentication((cfg =>
        {
            cfg.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            cfg.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })).AddJwtBearer(options =>
        {
            options.RequireHttpsMetadata = false;

            options.TokenValidationParameters = new TokenValidationParameters
            {
                ValidIssuer = Configuration["JwtSecurityToken:Issuer"],
                ValidAudience = Configuration["JwtSecurityToken:Audience"],
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtSecurityToken:Key"])),
                ValidateLifetime = true
            };
        });

3. open Configure of Startup.cs class and remove this lines of code :

app.UseJwtBearerAuthentication(.................)
and app.UseIdentity()

4 . paste this line of code

   app.UseAuthentication();

Source code are available in this repositories :

Asp.Net-Web-Api-Core-2.0-Integration-testing-using-EntityFrameworkCore-LocalDb-and-Xunit2

Asp.Net-Web-Api-Core-2.0-Integration-testing-using-InMemory-EntityFrameworkCore-Sqlite-and-Xunit2

Best regards

Gora LEYE

I'm a microsoft most valuable professional (MVP) .NET Architect and Technical Expert skills located in Paris (FRANCE). The purpose of this blog is mainly to post general .NET tips and tricks, www.masterconduite.com Gora LEYE

Support us

BMC logoBuy me a coffee