Skip to content

ITAGH-CODE/JsonWebTokenDotNetApi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Important points in order to use JWT in .NET 6 Web API

=> Install package : Microsoft.AspNetCore.Authentication.JwtBearer

=> Add the following in the Program.cs : 
    // Begin JWT Section
    builder.Services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    })
    .AddJwtBearer(options =>
    {
        options.TokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuer = false,
            ValidateAudience = false,
            ValidateIssuerSigningKey = false,
            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:key"]))
        };
    });
    builder.Services.AddTransient<IJwtAuthenticationService, JwtAuthenticationService>();
    // End JWT Section

=> Add the JWT Key in the appsetting.json which we use to Generate the token (Also the same to validate it).
    For Demo, I used this web site to generate key https://mkjwk.org/

About

JWT with .NET 6

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages