Skip to main content

Security β€” JWT, rate limiting and token revocation

JWT signing key#

The API refuses to start with an insecure signing configuration.

  • WebAPI/appsettings.json ships without a key (TokenOptions:SecurityKey is empty).
  • Core/Utilities/Security/Jwt/TokenOptionsValidator.cs throws at startup when the key is missing or shorter than 32 bytes (256 bits).
  • Keys that were published in this repository are rejected in every environment except Development.
  • appsettings.Development.json contains a public sample key β€” local development only.
  • appsettings.Docker.json ships without a key; the container must receive one.

Provide the key through configuration. Never commit it:

export TokenOptions__SecurityKey="$(openssl rand -base64 32)"dotnet watch run --project ./WebAPI/WebAPI.csproj
docker run -e ASPNETCORE_ENVIRONMENT=Production \           -e TokenOptions__SecurityKey="<your-secret>" ...

If a deployment ever used a key that was published here, treat it as compromised and rotate it.

Token lifetime#

TokenOptions:AccessTokenExpiration is in minutes (default 10). Besides the normal exp check, the JWT bearer handler rejects any token whose expiration is more than AccessTokenExpiration + 1 minute in the future, so long-lived forged tokens are refused.

ClaimPurpose
jtiunique token id β€” required for revocation
nameidentifieruser id
unique name identifiercitizen id (only when greater than 0)
nameuser full name
roleauthentication provider type

Operation claims (command, query, screen) are not carried in the token; the secured-operation aspect checks them per request.

Token revocation#

  • Every access token carries a jti claim; tokens without one are rejected.
  • POST /api/v1/auth/logout stores the caller's jti in ICacheManager until the token expires and clears the stored refresh token.
  • The default in-memory cache forgets revocation state on restart and does not share it between instances. For multi-instance deployments configure the Redis cache:
"CacheOptions": {  "Host": "localhost",  "Port": 6379,  "Password": "",  "Database": 0}

and register RedisCacheManager in Core/DependencyResolvers/CoreModule.cs (the default is MemoryCacheManager).

Rate limiting#

Configured under RateLimiting in appsettings.json and applied per controller action with [EnableRateLimiting("read" | "crud" | "auth")].

PolicyDefault limitWindowUsed by
auth10 requests1 minutelogin, refresh-token, verify
crud100 requests1 minutecreate / update / delete, including register and password changes
read200 requests1 minutelist / get actions

Rejected requests return HTTP 429 with a JSON body:

{ "statusCode": 429, "message": "Too many requests. Please try again later.", "retryAfter": 60 }

CORS#

The AllowOrigin policy currently allows any origin, method and header β€” convenient for local development with several client ports. Restrict it before exposing the API publicly.

Other defaults#

  • API versioning: default version 1.0, read from the route (/api/v1/...) or the x-dev-arch-version header.
  • Swagger: available outside Production at /swagger; use the Authorize button with a Bearer token.
  • Global exception handling: unhandled exceptions are turned into a JSON error response by GlobalExceptionMiddleware.
  • Health checks: /healthz (and /health) include database and Hangfire checks.

Regression tests#

TestFile
Key validationTests/Core/Security/Jwt/TokenOptionsValidatorTests.cs
Forged lifetimeTests/WebAPI/ForgedTokenLifetimeTests.cs
RevocationTests/WebAPI/TokenRevocationTests.cs