JwtServicePackage 10.0.10

There is a newer version of this package available.
See the version list below for details.
dotnet add package JwtServicePackage --version 10.0.10
                    
NuGet\Install-Package JwtServicePackage -Version 10.0.10
                    
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="JwtServicePackage" Version="10.0.10" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JwtServicePackage" Version="10.0.10" />
                    
Directory.Packages.props
<PackageReference Include="JwtServicePackage" />
                    
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 JwtServicePackage --version 10.0.10
                    
#r "nuget: JwtServicePackage, 10.0.10"
                    
#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 JwtServicePackage@10.0.10
                    
#: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=JwtServicePackage&version=10.0.10
                    
Install as a Cake Addin
#tool nuget:?package=JwtServicePackage&version=10.0.10
                    
Install as a Cake Tool

🔐 JwtServicePackage

A production-ready JWT authentication and token lifecycle engine for .NET, designed for security, scalability, and real-world distributed systems.

NuGet Version NuGet Downloads

Unlike traditional JWT setups that rely on static secrets and stateless validation only, this package introduces a full token security lifecycle system with:

  • Key rotation
  • Multi-key validation (zero-downtime rotation)
  • Refresh token lifecycle management
  • Token revocation (blacklisting)
  • Replay attack detection
  • Session control per user/device

Updates

Lastest updates

  • Updated EnableTokenReplayDetection to false as default
  • Added EnableTokenReplayDetectionMinutes for user configuration by default set to 5 minutes
  • EnableTokenReplayDetectionMinutes can be added to JwtSettings if not, by default will be false

New version: 10.0.10 is available with updates to address the above changes

IMPORTANT: Version 10.0.10 is the latest, 10.0.5 is available alternatively

🚀 Features

🔐 Authentication Core

  • Generate Access + Refresh token pairs
  • Claims-based identity support
  • Token decoding utilities

🔁 Token Lifecycle Management

  • Secure refresh token rotation
  • Per-user session limits
  • Token revocation (single or all sessions)
  • Device-aware session tracking

🧠 Security Enhancements

  • Replay attack detection (JTI tracking)
  • Token blacklisting
  • Hash-based refresh token storage
  • Per-user active session enforcement

🔄 Key Management System (NEW)

  • Automatic key generation (if not provided)
  • Rotating signing keys with KeyId (kid)
  • Multi-key validation for backward compatibility
  • Retains old keys until refresh-token expiry window ends
  • Zero-downtime key rotation

🧠 Architecture Overview

Client → JWT Middleware → JwtService → Controller

Key rotation ensures all valid keys remain usable during rotation windows.


📦 Installation

dotnet add package JwtServicePackage

⚙️ Configuration

Add to appsettings.json:

{
  "JwtSettings": {
    "SecretKey": "your-initial-secret-key-32chars-minimum",
    "Issuer": "your-app",
    "Audience": "your-app-users",
    "AccessTokenExpiryMinutes": 15,
    "RefreshTokenExpiryDays": 7,
    "EnableKeyRotation": true,
    "KeyRotationIntervalDays": 7,
    "EnableTokenReplayDetection": false,
    "EnableTokenReplayDetectionMinutes": 5,
    "EnableTokenBlacklisting": true,
    "MaxActiveTokensPerUser": 5
  }
}

🧩 Setup (Program.cs)

builder.Services.AddJwtAuthentication(builder.Configuration);
builder.Services.AddHttpContextAccessor();
app.UseAuthentication();
app.UseAuthorization();

🔑 Usage

Generate tokens:


var tokens = _jwtService.GenerateTokenPair("user-123");

Validate:
var result = _jwtService.ValidateAccessToken(token);

Refresh:
var newTokens = _jwtService.RefreshToken(refreshToken);

🔑 Generating Tokens

var tokens = _jwtService.GenerateTokenPair(
    userId: user.UserId.ToString(),
    customClaims: new Dictionary<string, object>
    {
        { ClaimTypes.Email, user.Email }
    },
    deviceInfo: "web",
    ipAddress: "127.0.0.1"
);

🔍 Validating Tokens

var result = _jwtService.ValidateAccessToken(token);

if (!result.IsValid)
{
    // handle invalid token
}

🔄 Refreshing Tokens

var newTokens = _jwtService.RefreshToken(refreshToken);

🚫 Revocation

_jwtService.RevokeToken(accessToken);
_jwtService.RevokeRefreshToken(refreshToken);
_jwtService.RevokeAllUserTokens(userId);

👤 Access Current User

Use claims via HttpContext:

var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

🔐 Security Model

JWT Middleware → cryptographic validation
JwtService → business security rules

Do NOT duplicate validation logic.


🔄 Background Cleanup

Token cleanup runs automatically via BackgroundService:

  • Removes expired refresh tokens
  • Clears old revoked tokens
  • Cleans replay tracking

📄 License

MIT License - free for commercial and personal use.


👨‍💻 Author

Created and Maintained by: Ethern-Myth

Product Compatible and additional computed target framework versions.
.NET 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. 
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
10.0.20 103 10/1/2026
10.0.10 118 8/28/2026
10.0.5 165 4/12/2026

Updated EnableTokenReplayDetection to be false by default in version 10.0.10, Read the notes changes