qckdev.AspNetCore.Authentication.JwtBearer
0.3.0-net10.1665
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
<PackageReference Include="qckdev.AspNetCore.Authentication.JwtBearer" Version="0.3.0-net10.1665" />
<PackageVersion Include="qckdev.AspNetCore.Authentication.JwtBearer" Version="0.3.0-net10.1665" />
<PackageReference Include="qckdev.AspNetCore.Authentication.JwtBearer" />
paket add qckdev.AspNetCore.Authentication.JwtBearer --version 0.3.0-net10.1665
#r "nuget: qckdev.AspNetCore.Authentication.JwtBearer, 0.3.0-net10.1665"
#:package qckdev.AspNetCore.Authentication.JwtBearer@0.3.0-net10.1665
#addin nuget:?package=qckdev.AspNetCore.Authentication.JwtBearer&version=0.3.0-net10.1665&prerelease
#tool nuget:?package=qckdev.AspNetCore.Authentication.JwtBearer&version=0.3.0-net10.1665&prerelease
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 | Versions 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. |
-
.NETCoreApp 3.1
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 3.1.32)
- Newtonsoft.Json (>= 13.0.1)
- System.IdentityModel.Tokens.Jwt (>= 6.34.0)
-
.NETStandard 2.0
-
net10.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 10.0.0)
-
net5.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 5.0.17)
- System.IdentityModel.Tokens.Jwt (>= 6.34.0)
-
net6.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 6.0.26)
-
net8.0
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 |