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" />
                    
Directory.Packages.props
<PackageReference Include="JwtFactory" />
                    
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 JwtFactory --version 2.0.0
                    
#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
                    
Install as a Cake Addin
#tool nuget:?package=JwtFactory&version=2.0.0
                    
Install as a Cake Tool

JwtFactory

A lightweight library for adding JWT-based authentication to .NET applications with minimal configuration.

NuGet License: MIT

Features

  • Simple setup with IServiceCollection extension methods
  • JWT token generation and validation
  • Built on Microsoft.IdentityModel.JsonWebTokens for 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 ID
  • GetUsername() - Get username
  • GetFullName() - Get full name
  • GetFirstName() - Get first name
  • GetLastName() - Get last name
  • GetRoles() - Get all roles
  • HasRole(string role) - Check if user has role

License

MIT

Product 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.

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 707 1/18/2026
1.0.2 1,306 6/28/2021
1.0.1 439 6/28/2021
1.0.0 563 6/15/2021

v2.0.0: Modernized to .NET 8/9, updated to IdentityModel 8.x, added IJwtProvider interface, improved performance with JsonWebTokenHandler, enabled nullable reference types.