JwtFactory 2.0.0
dotnet add package JwtFactory --version 2.0.0
NuGet\Install-Package JwtFactory -Version 2.0.0
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="JwtFactory" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JwtFactory" Version="2.0.0" />
<PackageReference Include="JwtFactory" />
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 JwtFactory --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: JwtFactory, 2.0.0"
#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 JwtFactory@2.0.0
#: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=JwtFactory&version=2.0.0
#tool nuget:?package=JwtFactory&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
JwtFactory
A lightweight library for adding JWT-based authentication to .NET applications with minimal configuration.
Features
- Simple setup with
IServiceCollectionextension methods - JWT token generation and validation
- Built on
Microsoft.IdentityModel.JsonWebTokensfor optimal performance - Configurable token expiry and clock skew
- Extension methods for extracting claims from
ClaimsPrincipal - Full nullable reference type support
- Targets .NET 8 and .NET 9
Installation
dotnet add package JwtFactory
Quick Start
1. Configure in Program.cs
// Using fluent builder
builder.Services.AddJwtProvider(jwt => jwt
.WithIssuer("https://myapp.com")
.WithAudience("https://myapp.com")
.WithSecretKey("your-secret-key-at-least-32-characters-long")
.WithDefaultExpiry(TimeSpan.FromHours(2)));
// Or using JwtInfo directly
builder.Services.AddJwtProvider(new JwtInfo
{
Issuer = "https://myapp.com",
Audience = "https://myapp.com",
SecretKey = "your-secret-key-at-least-32-characters-long"
});
2. Generate Tokens
public class AuthController : ControllerBase
{
private readonly IJwtProvider _jwtProvider;
public AuthController(IJwtProvider jwtProvider)
{
_jwtProvider = jwtProvider;
}
[HttpPost("login")]
public IActionResult Login(LoginRequest request)
{
// Validate credentials...
var user = new ClaimsUser
{
Id = "user-123",
Username = "john.doe",
FirstName = "John",
LastName = "Doe",
Roles = "Admin,User"
};
var token = _jwtProvider.GetUserToken(user);
return Ok(new { Token = token });
}
}
3. Protect Endpoints
[Authorize]
[HttpGet("protected")]
public IActionResult GetProtectedData()
{
var userId = User.GetUserId();
var username = User.GetUsername();
var roles = User.GetRoles();
return Ok(new { userId, username, roles });
}
4. Validate Tokens Manually (Optional)
var principal = _jwtProvider.ValidateToken(token);
if (principal != null)
{
var userId = principal.GetUserId();
}
Configuration Options
| Property | Description | Default |
|---|---|---|
Issuer |
Token issuer (required) | - |
Audience |
Token audience (required) | - |
SecretKey |
Signing key, min 32 chars (required) | - |
DefaultExpiry |
Default token lifetime | 1 hour |
ClockSkew |
Validation clock skew tolerance | 5 minutes |
Extension Methods
Extract claims from ClaimsPrincipal:
GetUserId()- Get user IDGetUsername()- Get usernameGetFullName()- Get full nameGetFirstName()- Get first nameGetLastName()- Get last nameGetRoles()- Get all rolesHasRole(string role)- Check if user has role
License
MIT
| 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 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 10.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.2)
- Microsoft.IdentityModel.JsonWebTokens (>= 8.15.0)
-
net8.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.23)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.2)
- Microsoft.IdentityModel.JsonWebTokens (>= 8.15.0)
-
net9.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 9.0.12)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.2)
- Microsoft.IdentityModel.JsonWebTokens (>= 8.15.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v2.0.0: Modernized to .NET 8/9, updated to IdentityModel 8.x, added IJwtProvider interface, improved performance with JsonWebTokenHandler, enabled nullable reference types.