qckdev.AspNetCore.Authentication.JwtBearer 0.3.0-net10.1665

This is a prerelease version of qckdev.AspNetCore.Authentication.JwtBearer.
dotnet add package qckdev.AspNetCore.Authentication.JwtBearer --version 0.3.0-net10.1665
                    
NuGet\Install-Package qckdev.AspNetCore.Authentication.JwtBearer -Version 0.3.0-net10.1665
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="qckdev.AspNetCore.Authentication.JwtBearer" Version="0.3.0-net10.1665" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="qckdev.AspNetCore.Authentication.JwtBearer" Version="0.3.0-net10.1665" />
                    
Directory.Packages.props
<PackageReference Include="qckdev.AspNetCore.Authentication.JwtBearer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add qckdev.AspNetCore.Authentication.JwtBearer --version 0.3.0-net10.1665
                    
#r "nuget: qckdev.AspNetCore.Authentication.JwtBearer, 0.3.0-net10.1665"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package qckdev.AspNetCore.Authentication.JwtBearer@0.3.0-net10.1665
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=qckdev.AspNetCore.Authentication.JwtBearer&version=0.3.0-net10.1665&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=qckdev.AspNetCore.Authentication.JwtBearer&version=0.3.0-net10.1665&prerelease
                    
Install as a Cake Tool

NuGet Version Quality Gate Code Coverage Azure Pipelines Status

qckdev.AspNetCore.Authentication.JwtBearer

Provides helpers to register JWT bearer schemes and keep token generation in sync with validation settings.

๐Ÿ“ฆ Packages

  • qckdev.AspNetCore.Authentication.JwtBearer: validation helpers plus generator wiring for the API schemes your services expose.
  • qckdev.AspNetCore.Authentication.JwtBearer.Swagger: a Swagger UI helper that captures your API-issued tokens and pre-authorizes the matching bearer scheme.

๐Ÿ› ๏ธ Installation

dotnet add package qckdev.AspNetCore.Authentication.JwtBearer

โšก Quick Start

1. Model the JWT settings you expect to read from configuration

{
  "OAuth2": {
    "Code": {
      "Key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "AccessExpireSeconds": 60
    },
    "Token": {
      "Key": "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy",
      "AccessExpireSeconds": 86400
    }
  }
}
sealed class JwtTokenConfiguration
{
    public string Key { get; set; } = string.Empty;
    public double? AccessExpireSeconds { get; set; }
}

Bind each section in Program.cs or Startup.cs using IConfiguration.GetSection(...).Get<JwtTokenConfiguration>() so the next steps share the same symmetric key plus optional lifetime.

2. Register the extension that wires validation + generator services

public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder, string authenticationScheme, JwtTokenConfiguration configuration)
{
    var key = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(configuration.Key));

    builder.Services.AddScoped<IJwtGeneratorService, JwtGeneratorService>();
    return builder.AddJwtBearer(authenticationScheme,
        options =>
        {
            options.RequireHttpsMetadata = false;
            options.SaveToken = true;
            options.TokenValidationParameters = new TokenValidationParameters()
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = key,
                ValidateAudience = false,
                ValidateIssuer = false,
                ValidateLifetime = true,
                ClockSkew = TimeSpan.Zero
            };
        },
        moreOptions =>
        {
            moreOptions.TokenLifeTimespan = configuration.AccessExpireSeconds.HasValue
                ? TimeSpan.FromSeconds(configuration.AccessExpireSeconds.Value)
                : (TimeSpan?)null;
        }
    );
}

This keeps the IJwtGeneratorService that depends on JwtBearerOptions in sync with what the middleware expects; the same TokenLifeTimespan used to validate tokens is applied when generating them.

3. Generate tokens from the configured options

sealed class JwtGeneratorService : IJwtGeneratorService
{
    IOptionsMonitor<JwtBearerOptions> JwtOptionsMonitor { get; }
    IOptionsMonitor<JwtBearerMoreOptions> JwtMoreOptionsMonitor { get; }

    public JwtGeneratorService(IOptionsMonitor<JwtBearerOptions> jwtOptionsMonitor, IOptionsMonitor<JwtBearerMoreOptions> jwtMoreOptionsMonitor)
    {
        JwtOptionsMonitor = jwtOptionsMonitor;
        JwtMoreOptionsMonitor = jwtMoreOptionsMonitor;
    }

    public Task<JwtToken> CreateTokenAsync(string scheme, string userName, IEnumerable<string> roles, IEnumerable<Claim> claims)
    {
        var jwtOptions = JwtOptionsMonitor.Get(scheme);
        var jwtMoreOptions = JwtMoreOptionsMonitor.Get(scheme);

        return Task.FromResult(
            JwtGenerator.CreateToken(
                jwtOptions.TokenValidationParameters.IssuerSigningKey,
                userName,
                roles,
                claims,
                jwtMoreOptions.TokenLifeTimespan)
        );
    }
}

Inject the service wherever you build tokens for clients, passing the same scheme string used to register the handler so the correct validation/key pair is selected.

4. Add both authentication schemes to the DI container

const string AUTHENTICATIONSCHEME_CODE = "Code";
const string AUTHENTICATIONSCHEME_TOKEN = "Bearer";

var jwtCodeConfiguration = Configuration.GetSection("OAuth2:Code").Get<JwtTokenConfiguration>();
var jwtTokenConfiguration = Configuration.GetSection("OAuth2:Token").Get<JwtTokenConfiguration>();

services.AddAuthentication(AUTHENTICATIONSCHEME_CODE)
    .AddJwtBearer(AUTHENTICATIONSCHEME_CODE, jwtCodeConfiguration);

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(AUTHENTICATIONSCHEME_TOKEN, jwtTokenConfiguration);

You can use [Authorize(AuthenticationSchemes = "...")] on controllers or actions if you mix multiple schemes in the same API.

๐Ÿงช Testing

This library includes comprehensive integration tests covering token validation, signature verification, and expiration handling.

8 integration tests validate the complete JWT Bearer authentication pipeline:

  • Public endpoint access
  • Challenge response (401 Unauthorized)
  • Valid and expired tokens
  • Token format validation
  • Bearer scheme and claims handling

For detailed testing documentation, see Integration Testing Guide.

๐Ÿค Contributing

Issues and pull requests are welcome! See the contribution guidelines (coming soon).

๐Ÿ“œ License

This project is licensed under the terms of the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.3.0-net10.1665 44 3/31/2026
0.3.0-net10.1664 35 3/30/2026
0.3.0-net10.1663 38 3/30/2026
0.3.0-net10.1662 32 3/30/2026
0.3.0-net10.1661 35 3/30/2026
0.3.0-net10.1657 40 3/30/2026
0.3.0-net10.1631 51 3/18/2026
0.2.0-alpha 528 8/19/2022
0.1.0-alpha 306 2/14/2022