Configuring JWT authentication

Now that we know why we're using JWTs, we need to make a few configuration changes to enable authentication and specify the mechanism we wish to use. We'll start by opening the Startup.cs file, and finding the following piece of code within it:

services.AddIdentity<AppUser, AppRole>()  .AddEntityFrameworkStores<EcommerceContext>()  .AddDefaultTokenProviders();

Directly beneath this section, add the following:

services.AddAuthentication(options =>{  options.DefaultAuthenticateScheme =    JwtBearerDefaults.AuthenticationScheme;  options.DefaultChallengeScheme =   JwtBearerDefaults.AuthenticationScheme;})

If we were using cookie-based authentication, we wouldn't need this code at all, but since we're using JWTs, we ...

Get ASP.NET Core 2 and Vue.js now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.