JwtRefreshTokenRotation 1.1.1

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

JWT Inactivity Middleware πŸ”’

NuGet CI License: AGPL v3

JWT Inactivity Middleware is a reusable ASP.NET Core package that enforces inactivity thresholds on JWT bearer tokens.
Unlike standard JWT expiration (exp), this middleware tracks last access time in Redis and rejects tokens that exceed a configurable inactivity window.


✨ Features

  • Inactivity threshold enforcement: Reject tokens idle longer than the configured duration.
  • Redis-backed tracking: Distributed cache ensures consistency across multiple API instances.
  • Sliding expiration: Active tokens remain valid as long as they’re used within the threshold.
  • Configurable options: Set inactivity threshold, Redis connection string, and JWT secret via JwtTokenRotationOptions.
  • JWT generator service: Issue tokens with jti claim for inactivity tracking.

✈️ Usage

Register to service

builder.Services.AddJwtTokenRotation(new JwtTokenRotationOptions
{
    InactivityThreshold = TimeSpan.FromMinutes(15),
    RedisConnectionString = "localhost:6379",
    JwtSecret = builder.Configuration["Jwt:Secret"]
});

app.UseJwtTokenRotation();

πŸ”’Best Practices

Always enforce both absolute expiration (exp) and inactivity threshold.

  • Use Redis TTL to auto-clean expired sessions.
  • Configure secrets via environment variables or appsettings.json.
  • Log jti, user ID, and inactivity rejections for auditing.
  • Keep middleware lightweight and stateless aside from Redis.

🎯 Contributing

Contributions are welcome!

  • Fork the repo
  • Create a feature branch
  • Submit a pull request
  • Please ensure all tests pass before submitting.
  • For major changes, open an issue first to discuss what you’d like to change.

πŸ›  Project Structure

JwtTokenMiddleware/
 β”œβ”€β”€ JwtTokenRotationMiddleware.cs   # Core middleware logic
 β”œβ”€β”€ JwtTokenRotationOptions.cs      # Configurable options
 β”œβ”€β”€ JwtTokenGenerator.cs            # Token generator service
 β”œβ”€β”€ JwtTokenRotationExtension.cs      # DI + middleware registration
 β”œβ”€β”€ JwtTokenMiddleware.csproj       # Library project file

JwtTokenMiddleware.Sample/
 β”œβ”€β”€ Program.cs                      # Demo API setup
 β”œβ”€β”€ Controllers/AuthController.cs   # Example login + token issuance
 β”œβ”€β”€ JwtTokenMiddleware.Sample.csproj

JwtTokenMiddleware.Test/
 β”œβ”€β”€ JwtTokenRotationMiddlewareTests.cs              # Unit tests for inactivity logic
 β”œβ”€β”€ JwtTokenMiddleware.Test.csproj

πŸ”ŒSequence Diagram

User                AuthController          JwtTokenGenerator        Redis                Middleware
 |                        |                        |                   |                        |
 |--- Login Request ----->|                        |                   |                        |
 |                        |--- GenerateTokenAsync->|                   |                        |
 |                        |                        |--- Create JWT ----|                        |
 |                        |                        |--- Store jti,lastAccess,TTL=TokenExpiration->|
 |                        |<-- JWT Token ----------|                   |                        |
 |<-- Token Response -----|                        |                   |                        |
 |                                                                                              |
 |--- API Request -------->------------------------>-------------------|----------------------->|
 |                        |                        |                   |                        |
 |                        |                        |                   |--- Get jti,lastAccess->|
 |                        |                        |                   |<-- lastAccess,TTL -----|
 |                        |                        |                   |                        |
 |                        |                        |                   |--- Compare inactivity--|
 |                        |                        |                   |--- If expired -> 401 --|
 |                        |                        |                   |                        |
 |                        |                        |                   |--- If valid: update lastAccess, TTL=remainingLifetime->|
 |                        |                        |                   |                        |
 |<-- Response (200/401)--|                        |                   |                        |


πŸ“ Change Log

🏷️ 1.1.0 
    - Added auto extend expiration feature.
    - When expiration is extended, a new token with an updated jti 
      should be returned via the 'X-New-Token' response header.
    - Delete old jti from Redis when token is regenerated.
    - Improved jwt generation by using SecurityTokenDescriptor
      rather than JwtSecurityToken.

🏷️ 1.0.0 
    - Initial release with core inactivity tracking features.

πŸ“¦ Installation

dotnet add package JwtRefreshTokenRotation --version 1.1.0
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
2.0.0 98 1/5/2026
1.1.2 98 1/5/2026
1.1.1 103 12/30/2025
1.1.0 105 12/29/2025
1.0.0 181 12/24/2025