Setting up JWT authentication in .NET core 2.0

user6728767

I'm in the process of migrating existing .NET core 1.1.4 code over to .NET core 2.0. It looks like we have to change it so that we add the authentication as a service in ConfigureService() instead of in the Configure() function.

We're currently using the following properties:

  • AutomaticAuthenticate
  • AutomaticChallenge
  • TokenValidationParameters.IssuerSigningKey
  • TokenValidationParameters.ValidAudence
  • TokenValidationParameters.ValidateIssuerSigningKey
  • TokenValidationParameters.ValidateLifetime
  • TokenValidationParameters.ValidIssuer

In the migration docs, the AddJwtBearer() has an options parameter with audience so thats what I used. However, I checked the interface of the options class and there doesn't seem to be any of the other values I need. However, There is a TokenValidationParameters property. Can I just instantiate the same token I have now and use that?

1.1.4 version:

app.UseAuthentication(new JwtBearerOptions
{
    AutomaticAuthenticate = true,
    AutomaticChallenge = true,
    TokenValidationParameters = new TokenValidationParameters
    {
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration.GetSection("AppConfiguration:Key").Value)),
        ValidAudience = Configuration.GetSection("AppConfiguration:SiteUrl").Value,
        ValidateIssuerSigningKey = true,
        ValidateLifetime = true,
        ValidIssuer = Configuration.GetSection("AppConfiguration:SiteUrl").Value
    }
});

2.0.0 version:

services
    .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options =>
    {
        var siteUrl = Configuration.GetSection("AppConfiguration:SiteUrl").Value;

        options.Audience = siteUrl;
        options.Authority = siteUrl;
        options.TokenValidationParameters = new TokenValidationParameters
        {
            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration.GetSection("AppConfiguration:Key").Value)),
            ValidateIssuerSigningKey = true,
            ValidateLifetime = true,
            ValidIssuer = Configuration.GetSection("AppConfiguration:SiteUrl").Value,
        };
    }); 

Does AutomaticAuthenticate and AutomaticChallenge become:

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme);
Mickaël Derriey

You're right, the AutomaticAuthenticate and AutomaticChallenge properties are gone in ASP.NET Core 2.0.

They're replaced by this overload of the AddAuthentication method in which you can specify the default authentication scheme.

The ASP.NET Core 1.x to ASP.NET Core 2.0 migration docs cover this.

Doing so means that, on every request, the authentication handler associated with the scheme (in your case, the JWT bearer token handler) will be run to try to authenticate the request.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Setting Up LinkedIn/OAuth Authentication in ASP.NET Core 2.0

Setting Up Social Authentication in ASP.NET Core 2.0

Firebase Authentication (JWT) with .NET Core

transfer JWT Authentication implementation from .net core 2 to asp.net web api 2

JWT Authentication and Swagger with .Net core 3.0

setting asp.net CORE 2 authentication cookie while using bearer token authentication

.NET Core 3 Cookie Authentication not setting identity

ASP .NET Core Identity default authentication vs JWT authentication

Cookie authentication not working properly with JWT authentication ASP.NET CORE

Issues setting up IHttpClientFactory in .NET Core 3.1

Setting up AWS Secrets Manager .Net Core

ASP.NET Core 5.0 JWT authentication is throws 401 code

JWT Authentication using a custom attribute in .NET Core Web API

.NET Core IssuerSigningKey from file for JWT Bearer Authentication

Is it required to use IdentityDbContext for JWT authentication under .NET Core 2.0 and EF?

ASP.NET Core Web API Facebook JWT Authentication

AuthorizeAttribute with JWT Token- Authentication in .NET Core 2.0

.Net Core JWT Authentication with custom API Key Middleware

ASP.NET Core JWT authentication changes Claims (sub)

ASP.NET Core JWT with Custom Authentication Type

ASP .NET CORE 2.2 JWT & Claims identity Authentication for Website

.net core - Jwt middleware authentication signing key being ignored

JWT Authentication for .net core 2.2 application not using Identity

ASP.NET Core JWT/Windows Authentication HTTP 400 Error

JWT authentication in SignalR (.NET Core) without passing token in Query String

asp.net core 2.0 Authorization is firing before authentication (JWT)

Authentication & Authorization in .Net Core 2.1 WepAPI using JWT

ASP NET Core JWT authentication allows expired tokens

JWT Authentication not working in ASP.NEt core web api