TPJ.Auth
10.0.1
dotnet add package TPJ.Auth --version 10.0.1
NuGet\Install-Package TPJ.Auth -Version 10.0.1
<PackageReference Include="TPJ.Auth" Version="10.0.1" />
<PackageVersion Include="TPJ.Auth" Version="10.0.1" />
<PackageReference Include="TPJ.Auth" />
paket add TPJ.Auth --version 10.0.1
#r "nuget: TPJ.Auth, 10.0.1"
#:package TPJ.Auth@10.0.1
#addin nuget:?package=TPJ.Auth&version=10.0.1
#tool nuget:?package=TPJ.Auth&version=10.0.1
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();
Cookie-based API example
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
ITokenSettingsbefore calling the auth extension methods soITokenHelperuses your configured values. AddTPJBearerAuth(...)reads JWTs from theAuthorizationheader.AddTPJBearerCookieAuth(...)also reads the JWT from the configured access-token cookie.ITokenHelper.GenerateToken(...)returns both the JWT and a refresh token.
| Product | Versions 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. |
-
net10.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 10.0.7)
- Microsoft.AspNetCore.Authentication.OpenIdConnect (>= 10.0.7)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.7)
- System.IdentityModel.Tokens.Jwt (>= 8.18.0)
- TPJ.Encrypt (>= 10.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Bug fix new TokenSettings(builder.Configuration) required fields