Rig.TUnit.Security.Jwt
0.1.0-beta.2
dotnet add package Rig.TUnit.Security.Jwt --version 0.1.0-beta.2
NuGet\Install-Package Rig.TUnit.Security.Jwt -Version 0.1.0-beta.2
<PackageReference Include="Rig.TUnit.Security.Jwt" Version="0.1.0-beta.2" />
<PackageVersion Include="Rig.TUnit.Security.Jwt" Version="0.1.0-beta.2" />
<PackageReference Include="Rig.TUnit.Security.Jwt" />
paket add Rig.TUnit.Security.Jwt --version 0.1.0-beta.2
#r "nuget: Rig.TUnit.Security.Jwt, 0.1.0-beta.2"
#:package Rig.TUnit.Security.Jwt@0.1.0-beta.2
#addin nuget:?package=Rig.TUnit.Security.Jwt&version=0.1.0-beta.2&prerelease
#tool nuget:?package=Rig.TUnit.Security.Jwt&version=0.1.0-beta.2&prerelease
Rig.TUnit.Security.Jwt
Fluent
JwtBuilderproducing tokens a realJwtBearerHandleraccepts — no test-only bypass. HS256 / RS256, kid rotation, negative builders.
What this package is
A JWT testing kit that produces tokens that pass through Microsoft's
real JwtBearerHandler validation pipeline. Supports HS256 / RS256
signing, kid rotation, issuer/audience binding, and — critically —
negative builders (BuildExpired, BuildNotYetValid, BuildTampered)
so tests can prove the server rejects bad tokens rather than silently
accepting them.
No AuthenticationScheme.AllowAnonymous bypass, no AuthorizeFilter
override — the production auth code runs unchanged.
When to use it
- Integration tests where an API requires a valid JWT.
- Verifying negative paths: expired, tampered, not-yet-valid, wrong audience.
- Kid-rotation testing for key-rollover scenarios.
- Not for: unit tests of claims transformation — use a
ClaimsPrincipalfactory directly.
Prerequisites
- .NET 10 SDK
- Project under test uses
Microsoft.AspNetCore.Authentication.JwtBearer.
Quick start
using Rig.TUnit.Security.Jwt.Builder;
var token = JwtBuilder.Create(new JwtBuilderOptions
{
DefaultIssuer = "my-issuer",
DefaultAudience = "my-audience",
})
.Subject("alice")
.Claim("role", "admin")
.ExpiresIn(TimeSpan.FromMinutes(15))
.SignedWithHs256(keyBytes)
.Build();
Options
| Property | Type | Default | Description |
|---|---|---|---|
DefaultIssuer |
string |
"rig.tunit" |
Issuer claim |
DefaultAudience |
string |
"test-audience" |
Audience claim |
DefaultTtl |
TimeSpan |
15m |
TTL when ExpiresIn not called |
DefaultAlgorithm |
string |
"HS256" |
Override in SignedWith… |
Fixture + helper APIs
Rig.TUnit.Security.Jwt.Builder.JwtBuilderRig.TUnit.Security.Jwt.Options.JwtBuilderOptionsRig.TUnit.Security.Jwt.Helpers.JwksKeyRotationHelper
Per-test isolation
Each JwtBuilder instance is standalone; no global state. Keys can be
per-test via IsolationKey.FromExecutionContext() as seed material.
Parallelism + performance
- HS256 signing: ~50 µs.
- RS256 signing: ~2 ms (key generation dominant).
- Safe under full parallelism.
Troubleshooting
- Token rejected with
IDX10503(signature invalid) — the signing key and the handler's validation key don't match. EnsureTokenValidationParameters.IssuerSigningKeyuses the same bytes. BuildExpiredtoken accepted — the handler'sClockSkewdefault is 5 minutes;BuildExpired(TimeSpan.FromMinutes(10))clears that window.
See docs/troubleshooting.md#jwt.
Provider quirks + edge cases
kidrotation:JwksKeyRotationHelper.SetCurrentKid("new")updates the JWKS endpoint's active key; tokens signed with the oldkidcontinue validating if the handler caches the full key set.- HS256 key material must be at least 256 bits (32 bytes); shorter keys are padded and the handler rejects them.
BuildTamperedflips one bit of the signature — the resulting token round-trips the header/claims fine and fails only at signature check.
Benchmarks
See JwtBenchmarks.cs;
baseline in benchmarks/baseline-005.json.
Related docs
- Architecture diagram
- Glossary
- Family base:
Rig.TUnit.Security
License
MIT. See LICENSE.
| 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
- Bogus (>= 35.6.1)
- Microsoft.Extensions.Configuration (>= 10.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Options (>= 10.0.0)
- Microsoft.Extensions.Options.DataAnnotations (>= 10.0.0)
- Microsoft.IdentityModel.JsonWebTokens (>= 8.3.0)
- Microsoft.IdentityModel.Tokens (>= 8.3.0)
- Rig.TUnit.Security (>= 0.1.0-beta.2)
- System.IdentityModel.Tokens.Jwt (>= 8.3.0)
- TUnit.Core (>= 1.34.5)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Rig.TUnit.Security.Jwt:
| Package | Downloads |
|---|---|
|
Rig.TUnit.All
Meta-package containing every Rig.TUnit.* package. DISCOURAGED — prefer per-feature or per-stack meta-packages (Rig.TUnit, Rig.TUnit.Microservices). |
|
|
Rig.TUnit.Security.OAuth
TUnit fixture for OAuth flows (auth-code+PKCE, client-credentials) against an in-process IdentityServer or stub provider. |
|
|
Rig.TUnit.Microservices
Meta-package: Core + Mediator + Grpc + Outbox + Tracing + Jwt + Seq — the opinionated microservice testing kit. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0-beta.2 | 41 | 4/27/2026 |
| 0.0.0-alpha.0.14 | 51 | 4/26/2026 |