Ozcorps.Jwt
8.1.0
dotnet add package Ozcorps.Jwt --version 8.1.0
NuGet\Install-Package Ozcorps.Jwt -Version 8.1.0
<PackageReference Include="Ozcorps.Jwt" Version="8.1.0" />
<PackageVersion Include="Ozcorps.Jwt" Version="8.1.0" />
<PackageReference Include="Ozcorps.Jwt" />
paket add Ozcorps.Jwt --version 8.1.0
#r "nuget: Ozcorps.Jwt, 8.1.0"
#:package Ozcorps.Jwt@8.1.0
#addin nuget:?package=Ozcorps.Jwt&version=8.1.0
#tool nuget:?package=Ozcorps.Jwt&version=8.1.0
Ozcorps.Jwt
JWT (JSON Web Token) authentication library for ASP.NET Core. Provides Options Pattern configuration, Swagger integration, and a full-featured token service.
Installation
dotnet add package Ozcorps.Jwt
<PackageReference Include="Ozcorps.Jwt" Version="8.1.0" />
Configuration
Add the following section to appsettings.json:
{
"Jwt": {
"Issuer": "YourApp",
"Audience": "YourApp.Users",
"Key": "your-super-secret-key-at-least-32-chars-long",
"ValidateIssuer": true,
"ValidateAudience": true,
"ValidateLifetime": true,
"ValidateIssuerSigningKey": true,
"ClockSkew": "00:00:00"
}
}
Security: Never commit real JWT keys to source control. Use environment variables or a secrets manager.
Configuration Reference
| Property | Type | Default | Description |
|---|---|---|---|
Issuer |
string |
— | Token issuer (required) |
Audience |
string |
— | Token audience (required) |
Key |
string |
— | HMAC-SHA256 signing key, min 32 characters (required) |
ClockSkew |
TimeSpan |
00:00:00 |
Clock skew tolerance for expiry validation |
ValidateIssuer |
bool |
true |
Whether to validate the issuer claim |
ValidateAudience |
bool |
true |
Whether to validate the audience claim |
ValidateLifetime |
bool |
true |
Whether to validate token expiry |
ValidateIssuerSigningKey |
bool |
true |
Whether to validate the signing key |
Quick Start
using Ozcorps.Jwt.Extensions;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddJwtAuthentication(builder.Configuration);
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.Run();
Swagger Integration
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
options.AddJwtSecurity();
});
AddJwtSecurity() adds the Bearer security scheme to Swagger UI so tokens can be entered directly in the UI.
IJwtTokenService
IJwtTokenService is registered as scoped and available via dependency injection.
public class AuthController(IJwtTokenService jwtTokenService) : ControllerBase
{
[HttpPost("login")]
public IActionResult Login([FromBody] LoginRequest request)
{
// validate credentials...
var token = jwtTokenService.GenerateToken(
userId: user.Id.ToString(),
username: user.Username,
roles: new[] { "User" },
expiration: DateTime.UtcNow.AddHours(8)
);
return Ok(new { token, expiresAt = jwtTokenService.GetTokenExpiration(token) });
}
}
API
| Method | Description |
|---|---|
GenerateToken(IEnumerable<Claim>, DateTime?) |
Generates a token from a custom claims list |
GenerateToken(string userId, string username, IEnumerable<string>? roles, DateTime?) |
Generates a token for a user with optional roles |
ValidateToken(string token) |
Validates a token and returns the ClaimsPrincipal, or null if invalid |
GetTokenExpiration(string token) |
Returns the token's expiry time, or null if unreadable |
Custom Section Name
// appsettings.json
{
"Auth": {
"Jwt": { "Issuer": "...", "Audience": "...", "Key": "..." }
}
}
// Program.cs
builder.Services.AddJwtAuthentication(builder.Configuration, sectionName: "Auth:Jwt");
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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 was computed. 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. |
-
net8.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.24)
- Microsoft.Extensions.Options.DataAnnotations (>= 8.0.0)
- Swashbuckle.AspNetCore (>= 6.7.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.