Pinqponq.Identity 1.0.2

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

Pinqponq.Identity

Generic auth primitives in one package: JWT issue/validate, ASP.NET Core JWT bearer authentication, refresh token issue/rotate/revoke, and password hash/verify. The package owns the crypto and lifecycle rules; storage for refresh tokens and revoked access tokens is left to the consuming application so this package stays free of a database dependency.

Install

dotnet add package Pinqponq.Identity

Requirements

  • .NET 8.0, 9.0, or 10.0
  • An IRefreshTokenStore implementation from your application if you use IRefreshTokenService (EF Core, Dapper, Redis, …)
  • An IAccessTokenRevocationStore implementation from your application if you need to revoke access tokens by jti (e.g. logout before natural expiry) — optional
  • A symmetric key of at least 32 bytes (256 bits) for HMAC signing, or a 2048-bit+ RSA key pair for RSA signing

Quick start

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Pinqponq.Identity.DependencyInjection;
using Pinqponq.Identity.Jwt;
using Pinqponq.Identity.Passwords;
using Pinqponq.Identity.RefreshTokens;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddPinqponqIdentity(
    configureJwt: jwt =>
    {
        jwt.Issuer = "https://auth.example.com";
        jwt.Audience = "example-api";
        jwt.Algorithm = JwtSigningAlgorithm.HmacSha256;
        jwt.SymmetricKey = builder.Configuration["Jwt:SymmetricKey"]; // >= 32 bytes
        jwt.Lifetime = TimeSpan.FromMinutes(15);
    },
    configureRefreshTokens: refresh =>
    {
        refresh.Lifetime = TimeSpan.FromDays(14);
        refresh.ReuseDetectionGrace = TimeSpan.FromSeconds(5);
    });

// Application-owned persistence for refresh tokens (see "Main types" below).
builder.Services.AddScoped<IRefreshTokenStore, MyRefreshTokenStore>();

var app = builder.Build();

app.MapPost("/login", async (
    LoginRequest request,
    IPasswordHasher passwordHasher,
    IJwtTokenGenerator jwtGenerator,
    IRefreshTokenService refreshTokenService,
    CancellationToken cancellationToken) =>
{
    var user = await MyUserLookup.FindByEmailAsync(request.Email, cancellationToken);
    var outcome = passwordHasher.Verify(user.PasswordHash, request.Password);
    if (outcome == PasswordVerificationOutcome.Failed)
    {
        return Results.Unauthorized();
    }

    if (outcome == PasswordVerificationOutcome.SuccessRehashNeeded)
    {
        user.PasswordHash = passwordHasher.Hash(request.Password);
        await MyUserLookup.SaveAsync(user, cancellationToken);
    }

    var claims = new[] { new Claim(ClaimTypes.NameIdentifier, user.Id) };
    var accessToken = jwtGenerator.GenerateToken(claims);
    var refreshToken = await refreshTokenService.IssueAsync(user.Id, cancellationToken);

    return Results.Ok(new { accessToken, refreshToken = refreshToken.Token });
});

app.MapPost("/refresh", async (
    RefreshRequest request,
    IRefreshTokenService refreshTokenService,
    CancellationToken cancellationToken) =>
{
    try
    {
        var rotated = await refreshTokenService.RotateAsync(request.RefreshToken, cancellationToken);
        return Results.Ok(new { refreshToken = rotated.Token });
    }
    catch (InvalidRefreshTokenException)
    {
        return Results.Unauthorized();
    }
});

app.Run();

Registering a password hash for a new user, and validating an incoming access token in a downstream handler:

public sealed class AccountService(IPasswordHasher passwordHasher)
{
    public string HashNewPassword(string plaintext) => passwordHasher.Hash(plaintext);
}

public sealed class ProfileEndpoint(IJwtTokenValidator jwtValidator)
{
    public async Task<ClaimsPrincipal?> AuthenticateAsync(string bearerToken, CancellationToken ct) =>
        await jwtValidator.ValidateAsync(bearerToken, ct);
}

ASP.NET Core bearer authentication

AddPinqponqJwtBearer registers ASP.NET Core's JWT bearer handler with validation parameters derived from JwtOptions — issuer, audience, signing key, lifetime validation and clock skew are never restated at the validation side, so a service that validates a token cannot drift away from the one that signed it.

// A service that issues tokens: AddPinqponqIdentity binds JwtOptions.
builder.Services.AddPinqponqIdentity(configureJwt: jwt => { /* ... */ });
builder.Services.AddPinqponqJwtBearer(bearer =>
{
    bearer.RequireHttpsMetadata = !builder.Environment.IsDevelopment();
});

// A service that only validates tokens: this overload binds JwtOptions itself,
// without the refresh token service or the password hasher.
builder.Services.AddPinqponqJwtBearer(configureJwt: jwt => { /* ... */ });

The configureBearer delegate is for settings that belong to the host rather than to the token contract; it runs after the derived parameters. The Bearer scheme is registered by default; pass authenticationScheme to use another name. The returned AuthenticationBuilder chains further schemes.

Configuration

JwtOptions (configured via configureJwt, validated on startup):

Option Default Notes
Issuer — Required. The iss claim and expected issuer during validation.
Audience — Required. The aud claim and expected audience during validation.
Lifetime 15 minutes Must be positive. Applied to newly issued access tokens.
ClockSkew 30 seconds Must be between 0 and 5 minutes.
Algorithm JwtSigningAlgorithm.HmacSha256 HmacSha256 or RsaSha256.
SymmetricKey — Required for HmacSha256. Must be at least 32 bytes (UTF-8 byte count).
RsaPrivateKeyPem — Required for RsaSha256. PEM (PKCS#8 or PKCS#1). Must be at least 2048 bits.
RsaPublicKeyPem — Optional for RsaSha256; derived from the private key if omitted.
ValidateIssuer / ValidateAudience / ValidateLifetime true Toggle individual validation checks.

RefreshTokenOptions (configured via configureRefreshTokens, optional):

Option Default Notes
Lifetime 14 days Must be positive.
TokenByteLength 32 Must be at least 32 (256 bits of entropy).
ReuseDetectionGrace 5 seconds Must be non-negative. See "Notes / behavior" below.

Main types

Type Lifetime Description
IJwtTokenGenerator Singleton GenerateToken(claims, issuedAt?) — issues a signed JWT from claims.
AddPinqponqJwtBearer DI extension Registers the ASP.NET Core JWT bearer handler with parameters derived from JwtOptions.
IJwtTokenValidator Scoped ValidateAsync(token, ct) — validates signature, issuer, audience, lifetime; returns a ClaimsPrincipal?.
IPasswordHasher Singleton (Pbkdf2PasswordHasher) Hash(password), Verify(hash, password) → PasswordVerificationOutcome.
IRefreshTokenService Scoped IssueAsync, RotateAsync, RevokeAsync. Requires the application to register IRefreshTokenStore.
IRefreshTokenStore App-provided AddAsync, FindByHashAsync, UpdateAsync, TryRevokeActiveAsync, CompleteRotationAsync, RevokeAllForSubjectAsync.
IAccessTokenRevocationService Scoped RevokeAccessTokenAsync(token, ct) — parses jti/exp and records the revocation. Requires IAccessTokenRevocationStore.
IAccessTokenRevocationStore App-provided (optional) RevokeAsync(jti, expiresAt, ct), IsRevokedAsync(jti, ct).
RefreshToken Model Persisted refresh-token record: TokenHash, Subject, CreatedAt, ExpiresAt, RevokedAt, ReplacedByTokenHash.
RefreshTokenResult Model Token (raw value, only ever surfaced once) + Descriptor (RefreshToken).
PasswordVerificationOutcome Enum Failed, Success, SuccessRehashNeeded.

Minimal IRefreshTokenStore implementation (in-memory, for illustration only — use a real database in production):

public sealed class MyRefreshTokenStore : IRefreshTokenStore
{
    private readonly ConcurrentDictionary<string, RefreshToken> _byHash = new();

    public Task AddAsync(RefreshToken token, CancellationToken ct = default)
    {
        _byHash[token.TokenHash] = token;
        return Task.CompletedTask;
    }

    public Task<RefreshToken?> FindByHashAsync(string tokenHash, CancellationToken ct = default) =>
        Task.FromResult(_byHash.GetValueOrDefault(tokenHash));

    public Task UpdateAsync(RefreshToken token, CancellationToken ct = default)
    {
        _byHash[token.TokenHash] = token;
        return Task.CompletedTask;
    }

    public Task<bool> TryRevokeActiveAsync(string tokenHash, DateTimeOffset revokedAt, CancellationToken ct = default)
    {
        if (_byHash.TryGetValue(tokenHash, out var token) && token.IsActive(revokedAt))
        {
            token.RevokedAt = revokedAt;
            return Task.FromResult(true);
        }

        return Task.FromResult(false);
    }

    public Task CompleteRotationAsync(string revokedTokenHash, RefreshToken replacement, CancellationToken ct = default)
    {
        if (_byHash.TryGetValue(revokedTokenHash, out var revoked))
        {
            revoked.ReplacedByTokenHash = replacement.TokenHash;
        }

        _byHash[replacement.TokenHash] = replacement;
        return Task.CompletedTask;
    }

    public Task RevokeAllForSubjectAsync(string subject, CancellationToken ct = default)
    {
        foreach (var token in _byHash.Values.Where(t => t.Subject == subject))
        {
            token.RevokedAt ??= DateTimeOffset.UtcNow;
        }

        return Task.CompletedTask;
    }
}

Notes / behavior

  • Refresh tokens are never stored raw. Only a SHA-256 hash is persisted; the raw value is returned exactly once, from IssueAsync/RotateAsync.
  • Rotation is a two-step, atomic contract. RotateAsync calls TryRevokeActiveAsync to compare-and-set the presented token to revoked, then CompleteRotationAsync to persist the replacement and link ReplacedByTokenHash on the ancestor — implementations must perform the add+link step as a single transaction/critical section, since a crash between them would leave a revoked token without a replacement link and silently disable reuse detection.
  • Reuse detection with a grace window. ReuseDetectionGrace (default 5 seconds) tolerates a concurrent double-submit of the same token immediately after rotation without punishing the caller. Once that grace window has elapsed, presenting an already-rotated token again is treated as token theft and calls RevokeAllForSubjectAsync, invalidating the entire token family for that subject.
  • IRefreshTokenStore is optional at registration time. Applications that only need JWT issuance/validation and password hashing can call AddPinqponqIdentity without ever registering a store. IRefreshTokenService is registered via a factory (not a type registration) specifically so that ASP.NET Core's container validation in Development does not abort startup for those applications — the missing store is only reported as an InvalidOperationException the first time IRefreshTokenService is resolved.
  • IAccessTokenRevocationStore follows the same pattern for applications that want immediate logout (revoke by jti before natural exp) — it's entirely optional, and the error also only surfaces on first resolve of IAccessTokenRevocationService.
  • IJwtTokenGenerator is a singleton; IJwtTokenValidator is scoped so an application's own scoped IAccessTokenRevocationStore can be consumed without a captive-dependency problem.
  • Target frameworks: net8.0, net9.0, net10.0.

Samples

Try this package in the browser via Pinqponq.Playground — see samples/README.md.

Repository

https://github.com/pinqponq/pinqnuqets

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
1.0.2 933 9/29/2026
1.0.1 733 8/7/2026
1.0.0 131 8/7/2026