A lightweight library for generating and managing JWT tokens in .NET applications. Supports access, refresh, and ID tokens with full .NET 9.0 compatibility.
$ dotnet add package Identity.Jwt.Token.ManagerThe Token Management System is a robust and flexible .NET library for managing JWT access tokens, ID tokens, and refresh tokens using RSA (RS256) or HMAC (HS256). It simplifies the process of secure token generation, validation, and expiration handling — ideal for building authentication and authorization systems in modern web, mobile, or API-based applications.
JwtTokenOptionModel or RsaTokenOptions).⚠️ Security Tip: It's highly recommended to use environment variables to store sensitive values like file paths and keys instead of placing them directly in
appsettings.json.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"RsaTokenOptions": {
"PrivateKeyPath": "/path/to/private.pem", // 🔐 Prefer setting via environment variable
"PublicKeyPath": "/path/to/public.pem", // 🔐 Prefer setting via environment variable
"Issuer": "https://auth.yourdomain.com",
"Audience": "https://api.yourdomain.com",
"AccessTokenLifetime": "00:30:00",
"RefreshTokenLifetime": "7.00:00:00",
"IdTokenLifetime": "00:15:00"
}
}
var claims = new List<Claim>
{
new(JwtRegisteredClaimNames.Sub, "user123"),
new(JwtRegisteredClaimNames.Email, "user@example.com"),
};
string accessToken = tokenManager.GenerateAccessToken(claims);
RefreshTokenModel refreshToken = tokenManager.GenerateRefreshToken();
string idToken = tokenManager.GenerateIdToken(claims);
bool isValid = tokenManager.ValidateRefreshToken(refreshTokenFromClient);
HMAC (HS256), make sure the SecretKey is long and random (at least 256 bits).{
"accessToken": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...",
"idToken": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...",
"refreshToken": "af13uZ0-SOME-BASE64-VALUE-0qZn",
"expiresIn": 1800
}
This is the most secure option, using a private key to sign tokens and a public key to validate them.
Program.cs Example
var rsaPublic = RSA.Create();
rsaPublic.ImportFromPem(File.ReadAllText(configuration["RsaTokenOptions:PublicKeyPath"]));
builder.Services.Configure<RsaTokenOptions>(configuration.GetSection("RsaTokenOptions"));
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = true;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
RequireExpirationTime = true,
RequireSignedTokens = true,
ValidateIssuerSigningKey = true,
ClockSkew = TimeSpan.Zero,
ValidIssuer = configuration["RsaTokenOptions:Issuer"],
ValidAudience = configuration["RsaTokenOptions:Audience"],
IssuerSigningKey = new RsaSecurityKey(rsaPublic)
};
});
Use this option if you're not using RSA keys. It's easier to set up but the SecretKey must remain private and secure.
Program.cs Example
var secretKey = configuration["JwtTokenOptionModel:SecretKey"];
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));
builder.Services.Configure<TokenOptionModel>(configuration.GetSection("TokenOptionModel"));
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = true;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
RequireExpirationTime = true,
RequireSignedTokens = true,
ValidateIssuerSigningKey = true,
ClockSkew = TimeSpan.Zero,
ValidIssuer = configuration["JwtTokenOptionModel:Issuer"],
ValidAudience = configuration["JwtTokenOptionModel:Audience"],
IssuerSigningKey = key
};
});
Program.cs// RSA (Asymmetric)
builder.Services.Configure<RsaTokenOptions>(
builder.Configuration.GetSection("RsaTokenOptions"));
builder.Services.AddScoped<RsaTokenManager>();
// HMAC (Symmetric)
builder.Services.Configure<TokenOptionModel>(
builder.Configuration.GetSection("JwtTokenOptionModel"));
builder.Services.AddScoped<JwtTokenManager>();
Install the package via NuGet:
dotnet add package Identity.Jwt.Token.Manager