TPJ.Auth 10.0.1

dotnet add package TPJ.Auth --version 10.0.1
                    
NuGet\Install-Package TPJ.Auth -Version 10.0.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="TPJ.Auth" Version="10.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TPJ.Auth" Version="10.0.1" />
                    
Directory.Packages.props
<PackageReference Include="TPJ.Auth" />
                    
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 TPJ.Auth --version 10.0.1
                    
#r "nuget: TPJ.Auth, 10.0.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 TPJ.Auth@10.0.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=TPJ.Auth&version=10.0.1
                    
Install as a Cake Addin
#tool nuget:?package=TPJ.Auth&version=10.0.1
                    
Install as a Cake Tool

TPJ.Auth

TPJ.Auth is a small helper package for issuing JWT access tokens, generating refresh tokens, and wiring JWT bearer authentication into ASP.NET Core API applications.

Install

dotnet add package TPJ.Auth

Configuration

Add the token settings to appsettings.json:

{
  "TPJ": {
    "Auth": {
      "Issuer": "MyApi",
      "Audience": "MyApi.Client",
      "SecretKey": "a-long-random-secret-key-used-to-sign-jwt-tokens",
      "Expiration": {
        "Hours": "0",
        "Minutes": "30",
        "Seconds": "0"
      },
      "RequireHttps": "true",
      "RefreshToken": {
        "Length": "32",
        "ExpiryDays": "7"
      },
      "Cookie": {
        "Path": "/",
        "AccessTokenKey": "access_token",
        "RefreshTokenKey": "refresh_token",
        "SameSite": "Strict",
        "Secure": "true",
        "HttpOnly": "true"
      }
    }
  }
}

Expiration is now configured with Hours, Minutes, and Seconds under TPJ:Auth:Expiration.

Bearer token API example

This example returns the JWT to the client and expects requests to send Authorization: Bearer <token>.

Program.cs

using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using TPJ.Auth;

var builder = WebApplication.CreateBuilder(args);

var tokenSettings = new TokenSettings(builder.Configuration);

builder.Services.AddSingleton<ITokenSettings>(tokenSettings);
builder.Services.AddTPJBearerAuth(tokenSettings.DefaultJwtBearerOptions());
builder.Services.AddAuthorization();

var app = builder.Build();

app.UseAuthentication();
app.UseAuthorization();

app.MapPost("/login", (ITokenHelper tokenHelper) =>
{
    var token = tokenHelper.GenerateToken(
        userName: "alice",
        userClaims:
        [
            new Claim(ClaimTypes.Name, "alice"),
            new Claim(ClaimTypes.Email, "alice@example.com")
        ],
        roles: ["User"]);

    return Results.Ok(new
    {
        accessToken = token.Token,
        expiresUtc = token.ExpirationDateTimeUtc,
        refreshToken = token.RefreshToken,
        refreshTokenExpiresUtc = token.RefreshTokenExpirationDateTimeUtc
    });
});

app.MapGet("/secure", [Authorize] (ClaimsPrincipal user) =>
{
    return Results.Ok(new
    {
        message = "Authenticated request",
        name = user.Identity?.Name,
        subject = user.FindFirst("sub")?.Value
    });
});

app.Run();

This example stores the access token and refresh token in cookies. Use this when your API and client are designed to authenticate with cookies instead of sending the bearer token manually.

Program.cs

using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using TPJ.Auth;

var builder = WebApplication.CreateBuilder(args);

var tokenSettings = new TokenSettings(builder.Configuration);

builder.Services.AddSingleton<ITokenSettings>(tokenSettings);
builder.Services.AddTPJBearerCookieAuth(tokenSettings);
builder.Services.AddAuthorization();

var app = builder.Build();

app.UseAuthentication();
app.UseAuthorization();

app.MapPost("/login", (HttpContext httpContext, ITokenHelper tokenHelper) =>
{
    var token = tokenHelper.GenerateToken(
        userName: "alice",
        userClaims: [new Claim(ClaimTypes.Name, "alice")],
        roles: ["User"]);

    tokenHelper.CreateAccessTokenCookie(
        httpContext.Request,
        token.Token,
        token.ExpirationDateTimeUtc);

    tokenHelper.CreateRefreshTokenCookie(
        httpContext.Request,
        token.RefreshToken,
        token.RefreshTokenExpirationDateTimeUtc);

    return Results.Ok(new
    {
        message = "Authentication cookies created",
        expiresUtc = token.ExpirationDateTimeUtc
    });
});

app.MapPost("/logout", [Authorize] (HttpContext httpContext, ITokenHelper tokenHelper) =>
{
    tokenHelper.DeleteAccessTokenCookie(httpContext.Request);
    tokenHelper.DeleteRefreshTokenCookie(httpContext.Request);

    return Results.NoContent();
});

app.MapGet("/secure", [Authorize] () => Results.Ok(new { message = "Authenticated with cookie or bearer token" }));

app.Run();

Notes

  • Register ITokenSettings before calling the auth extension methods so ITokenHelper uses your configured values.
  • AddTPJBearerAuth(...) reads JWTs from the Authorization header.
  • AddTPJBearerCookieAuth(...) also reads the JWT from the configured access-token cookie.
  • ITokenHelper.GenerateToken(...) returns both the JWT and a refresh token.
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.1 43 5/10/2026
10.0.0 45 5/10/2026

Bug fix new TokenSettings(builder.Configuration) required fields