Upgrading JWT Bearer for ASP.net Core 2.0 Final

Changes to Authentication

Alright, now that ASP.net Core 2.0 has been finalized it is time to upgrade from 1.1. I decided to jump in and upgrade one of my projects. The steps to upgrade were pretty simple but only ran into one issue, a small change to the Authentication setup. It wasn't major change but enough to cause a little research.

Assumptions

I am assuming you have already tried to upgrade your project to .NET Core 2.0. It is one of the simpler upgrades I have done. Essentially all I had to do was install the .NET 2.0 framework from here (also make sure you are running Visual Studio 2017 15.3). And then I upgraded all my nuget packages to 2.0. Yes that's it for the 2.0 upgrade!

Updating your Authentication Code

With ASP.net Core 1.1 the Authorization was configured in your startup Configure method and should look something similar to this:

OLD CODE:

var options = new JwtBearerOptions  
{
    Authority = Configuration.GetSection("Domain").Value,
    TokenValidationParameters =
    {
        ValidIssuer = Configuration.GetSection("Domain").Value,
        ValidAudience = Configuration.GetSection("ClientID").Value,
        IssuerSigningKey = new SymmetricSecurityKey(keyAsBytes)
    }
};

app.UseJwtBearerAuthentication(options);  

Now you will need to move your code to the ConfigureServices method of your startup and change it to something like this:

NEW CODE:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)  
    .AddJwtBearer(o =>
{
    o.Authority = Configuration.GetSection("Domain").Value;
    o.TokenValidationParameters = new TokenValidationParameters
    {
        ValidIssuer = Configuration.GetSection("Domain").Value,
        ValidAudience = Configuration.GetSection("ClientID").Value,
        IssuerSigningKey = new SymmetricSecurityKey(keyAsBytes)
    };
});

And now that you have your services configured, you will need to tell ASP.net to use Authentication which now goes in your Configure method so it will look something like this:

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

That's it! So far this has been one of the easier upgrades I have done. Hope this saves you a little time compared to digging through the articles on how to do this.