Introduction

JwtBearer token authentication is a JSON Web Token-based authentication method used to verify a user’s identity. It is a stateless authentication approach well suited to Web APIs and web applications.

Installing the NuGet package

dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer

Building your own Identity API endpoints

builder.Services.AddIdentity<User, Role>(options =>
{
    options.SignIn.RequireConfirmedAccount = false;
    options.Password.RequireDigit = false;
    options.Password.RequireLowercase = false;
    options.Password.RequireUppercase = false;
    options.Password.RequireNonAlphanumeric = false;
    options.Password.RequiredLength = 5;
}).AddEntityFrameworkStores<IdentityServiceDbContext>();

Validating tokens with JwtBearer


builder.Services.AddAuthentication(options =>
{
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultSignInScheme = CustomJwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
    options.TokenValidationParameters.ValidateIssuer = false;
    options.TokenValidationParameters.ValidateAudience = false;
    options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(Encoding.Default.GetBytes(issuerSigningKey));

})

A custom authentication handler

public class CustomJwtBearerDefaults

public class CustomJwtBearerOptions

public class CustomJwtBearerHandler

public class CustomJwtBearerExtensions

Configuring token generation

builder.Services.AddAuthentication().AddJwtBearer().AddCustomJwtBearer(options =>
{
    options.IssuerSigningKey = issuerSigningKey;
    options.SecurityAlgorithm = SecurityAlgorithms.HmacSha256;
});

Running the pgAdmin management tool in a container


docker pull dpage/pgadmin4

docker run --name pgadmin -e PGADMIN_DEFAULT_EMAIL=test@test.com -e PGADMIN_DEFAULT_PASSWORD=test -e TZ=Asia/Shanghai -d -p 5050:80 dpage/pgadmin4