Dot Tutorials
.Net 7

JWT Authentication Improvements & Automatic Configuration in .Net 7

Microsoft Announced .Net 7 Preview 5 with some great improvements. Here’s what’s new in Preview 5.

  • JWT Authentication Improvements and Automatic configuration.
  • Minimal API’s parameter binding support for argument list simplification.

I just installed Preview 5 to check JWT Authentication Configuration. If you also want to update your existing project to .Net 7 preview 5, here’s the quick way.

Update all Microsoft.AspNetCore.* package references to 7.0.0-preview.5.*.
Update all Microsoft.Extensions.* package references to 7.0.0-preview.5.*.

JWT Authentication Configuration Before .Net 7

Microsoft Stated they have received feedbacks that Configuring JWT in ASP.NET Core project is one of the hardest part of writing API. It required many steps including adding middleware at startup process & configuring services.

.Net Team knows the importance of JWT configuration in securing API that’s why they improved & simplified the process of configuring JWT Authentication in ASP.NET Core project.

Simplified JWT Configuration

Authentication options can now be configured automatically directly from the configuration system, due to the addition of a default configuration section when configuring via the new Authentication property on WebApplicationBuilder

var builder = WebApplication.CreateBuilder(args);

builder.Authentication.AddJwtBearer(); // New property

var app = builder.Build();

Setting up JWT Authentication using this new property will automatically add required middleware in a same way that WebApplicationBuilder does for routing.

On top of that, individual authentication schemes will set options parameters from appsettings.json automatically. That always help developers to configure parameters between development & production environment.

Here’s how appsettings.json file will look after adding authentication options.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Authentication": {
    "DefaultScheme" : "JwtBearer",
    "Schemes": {
      "JwtBearer": {
        "Audiences": [ "http://localhost:5000", "https://localhost:5001" ],
        "ClaimsIssuer": "user-jwt-here"
      }
    }
  }
}

It’s all about configuring JWT using .Net 7 preview 5. I haven’t tested other improvements coming with preview 5 but I can say that this improvement will really help developers to save their project setup time.

If you want to read about what’s coming with .Net 7. Please check this.

– What’s coming with .NET 7 ?

Author

I'm passionate about learning new technologies as well as mentoring and helping others get started with their programming career. This blog is my way of giving back to the Community.

Write A Comment