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
<PackageReference Include="Pinqponq.Identity" Version="1.0.2" />
<PackageVersion Include="Pinqponq.Identity" Version="1.0.2" />
<PackageReference Include="Pinqponq.Identity" />
paket add Pinqponq.Identity --version 1.0.2
#r "nuget: Pinqponq.Identity, 1.0.2"
#:package Pinqponq.Identity@1.0.2
#addin nuget:?package=Pinqponq.Identity&version=1.0.2
#tool nuget:?package=Pinqponq.Identity&version=1.0.2
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
IRefreshTokenStoreimplementation from your application if you useIRefreshTokenService(EF Core, Dapper, Redis, …) - An
IAccessTokenRevocationStoreimplementation from your application if you need to revoke access tokens byjti(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.
RotateAsynccallsTryRevokeActiveAsyncto compare-and-set the presented token to revoked, thenCompleteRotationAsyncto persist the replacement and linkReplacedByTokenHashon 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 callsRevokeAllForSubjectAsync, invalidating the entire token family for that subject. IRefreshTokenStoreis optional at registration time. Applications that only need JWT issuance/validation and password hashing can callAddPinqponqIdentitywithout ever registering a store.IRefreshTokenServiceis 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 anInvalidOperationExceptionthe first timeIRefreshTokenServiceis resolved.IAccessTokenRevocationStorefollows the same pattern for applications that want immediate logout (revoke byjtibefore naturalexp) — it's entirely optional, and the error also only surfaces on first resolve ofIAccessTokenRevocationService.IJwtTokenGeneratoris a singleton;IJwtTokenValidatoris scoped so an application's own scopedIAccessTokenRevocationStorecan be consumed without a captive-dependency problem.- Target frameworks:
net8.0,net9.0,net10.0.
Related packages
- Pinqponq.Identity.Otp — one-time passcodes over email/SMS, for step-up or passwordless flows.
- Pinqponq.Auth.Totp — RFC 6238 TOTP-based 2FA.
- Pinqponq.Auth.Sso.Abstractions and Pinqponq.Auth.Sso.Google — external identity providers that can feed into the same JWT/refresh-token issuance shown above.
Samples
Try this package in the browser via Pinqponq.Playground — see samples/README.md.
Repository
| 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. |
-
net10.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 10.0.10)
- Microsoft.Extensions.Configuration (>= 10.0.10)
- Microsoft.Extensions.DependencyInjection (>= 10.0.10)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.10)
- Microsoft.Extensions.Identity.Core (>= 10.0.10)
- Microsoft.Extensions.Logging (>= 10.0.10)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.10)
- Microsoft.Extensions.Options (>= 10.0.10)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.10)
- Microsoft.IdentityModel.JsonWebTokens (>= 8.19.2)
- Microsoft.IdentityModel.Tokens (>= 8.19.2)
-
net8.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.11)
- Microsoft.Extensions.DependencyInjection (>= 8.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Identity.Core (>= 8.0.11)
- Microsoft.Extensions.Logging (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.IdentityModel.JsonWebTokens (>= 8.19.2)
- Microsoft.IdentityModel.Tokens (>= 8.19.2)
-
net9.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 9.0.18)
- Microsoft.Extensions.DependencyInjection (>= 9.0.18)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.18)
- Microsoft.Extensions.Identity.Core (>= 9.0.18)
- Microsoft.Extensions.Logging (>= 9.0.18)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.18)
- Microsoft.Extensions.Options (>= 9.0.18)
- Microsoft.IdentityModel.JsonWebTokens (>= 8.19.2)
- Microsoft.IdentityModel.Tokens (>= 8.19.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.