Skip to content

Authentication

This page covers Cookie, Bearer Token, and JWT authentication settings in NpgsqlRest.

Overview

NpgsqlRest supports multiple authentication methods that can be used together:

  • Cookie Authentication - Traditional session-based authentication
  • Microsoft Bearer Token Authentication - Proprietary encrypted token authentication (ASP.NET Core specific)
  • JWT Authentication - Industry-standard JSON Web Token authentication (RFC 7519)
  • External OAuth Providers - Google, LinkedIn, GitHub, Microsoft, Facebook (see External OAuth)
json
{
  "Auth": {
    "CookieAuth": false,
    "BearerTokenAuth": false,
    "JwtAuth": false,
    "External": {
      "Enabled": false
    }
  }
}

Cookie authentication provides session-based authentication using HTTP cookies.

json
{
  "Auth": {
    "CookieAuth": true,
    "CookieAuthScheme": null,
    "CookieValidDays": 14,
    "CookieName": null,
    "CookiePath": null,
    "CookieDomain": null,
    "CookieMultiSessions": true,
    "CookieHttpOnly": true
  }
}
SettingTypeDefaultDescription
CookieAuthboolfalseEnable cookie authentication.
CookieAuthSchemestringnullAuthentication scheme name. Uses "Cookies" if null (from CookieAuthenticationDefaults.AuthenticationScheme).
CookieValidDaysint14Number of days the cookie remains valid.
CookieNamestringnullCustom name for the authentication cookie. Uses default if null.
CookiePathstringnullPath scope for the cookie. Uses default if null.
CookieDomainstringnullDomain scope for the cookie. Uses default if null.
CookieMultiSessionsbooltrueAllow multiple concurrent sessions for the same user.
CookieHttpOnlybooltrueMake cookie accessible only via HTTP (not JavaScript).

When CookieHttpOnly is true (recommended), the cookie cannot be accessed by client-side JavaScript, protecting against XSS attacks.

TIP

For production, always use HTTPS and consider setting CookieDomain to your specific domain.

Microsoft Bearer Token Authentication

Microsoft Bearer Token authentication provides stateless token-based authentication using ASP.NET Core's proprietary encrypted token format. This is suitable for single ASP.NET Core applications.

json
{
  "Auth": {
    "BearerTokenAuth": true,
    "BearerTokenAuthScheme": null,
    "BearerTokenExpireHours": 1,
    "BearerTokenRefreshPath": "/api/token/refresh"
  }
}

Bearer Token Settings Reference

SettingTypeDefaultDescription
BearerTokenAuthboolfalseEnable Microsoft bearer token authentication.
BearerTokenAuthSchemestringnullAuthentication scheme name. Uses "BearerToken" if null (from BearerTokenDefaults.AuthenticationScheme).
BearerTokenExpireHoursint1Number of hours before the token expires.
BearerTokenRefreshPathstring"/api/token/refresh"Endpoint path for refreshing tokens.

Token Refresh

To refresh a Microsoft bearer token, POST to the configured refresh path:

http
POST /api/token/refresh
Content-Type: application/json

{
  "refresh": "{{refreshToken}}"
}

JWT Authentication

New in 3.2.1

JWT authentication was added in version 3.2.1.

JWT (JSON Web Token) authentication provides industry-standard token-based authentication (RFC 7519). Unlike Microsoft Bearer Token authentication, JWT tokens are interoperable and can be used with any system that supports JWT.

json
{
  "Auth": {
    "JwtAuth": true,
    "JwtSecret": "your-secret-key-at-least-32-characters-long",
    "JwtIssuer": "your-app",
    "JwtAudience": "your-api",
    "JwtExpireMinutes": 60,
    "JwtRefreshExpireDays": 7,
    "JwtValidateIssuer": true,
    "JwtValidateAudience": true,
    "JwtValidateLifetime": true,
    "JwtValidateIssuerSigningKey": true,
    "JwtClockSkew": "5m",
    "JwtRefreshPath": "/api/jwt/refresh"
  }
}

JWT Settings Reference

SettingTypeDefaultDescription
JwtAuthboolfalseEnable JWT authentication.
JwtAuthSchemestringnullAuthentication scheme name. Uses "Bearer" if null (from JwtBearerDefaults.AuthenticationScheme).
JwtSecretstringnullSecret key for signing tokens. Must be at least 32 characters for HS256.
JwtIssuerstringnullToken issuer (iss claim).
JwtAudiencestringnullToken audience (aud claim).
JwtExpireMinutesint60Access token expiration in minutes.
JwtRefreshExpireDaysint7Refresh token expiration in days.
JwtValidateIssuerboolfalseValidate the issuer claim. Set to true if JwtIssuer is configured.
JwtValidateAudienceboolfalseValidate the audience claim. Set to true if JwtAudience is configured.
JwtValidateLifetimebooltrueValidate token expiration.
JwtValidateIssuerSigningKeybooltrueValidate the signing key.
JwtClockSkewstring"5m"Clock tolerance for expiration validation. Uses interval format.
JwtRefreshPathstring"/api/jwt/refresh"Endpoint path for refreshing JWT tokens.

Login Response

When JWT authentication is enabled and a login endpoint returns successfully, the response includes:

json
{
  "accessToken": "eyJhbG...",
  "refreshToken": "eyJhbG...",
  "tokenType": "Bearer",
  "expiresIn": 3600,
  "refreshExpiresIn": 604800
}

Token Refresh

To refresh a JWT token, POST to the configured refresh path (default: /api/jwt/refresh):

http
POST /api/jwt/refresh
Content-Type: application/json

{
  "refreshToken": "eyJhbG..."
}

The response returns a new access token and refresh token pair.

JWT vs Microsoft Bearer Token

FeatureMicrosoft Bearer TokenJWT
Token FormatProprietary, encryptedIndustry-standard (RFC 7519)
InteroperabilityASP.NET Core onlyAny system supporting JWT
Token InspectionOpaqueCan be decoded at jwt.io
Use CaseSingle ASP.NET appCross-service, microservices

Security

Store your JwtSecret securely. Use environment variables in production:

json
{
  "Auth": {
    "JwtSecret": "{JWT_SECRET}"
  }
}

Complete Examples

json
{
  "Auth": {
    "CookieAuth": true,
    "CookieValidDays": 30,
    "CookieHttpOnly": true,
    "CookieMultiSessions": false
  }
}

JWT Authentication

json
{
  "Auth": {
    "JwtAuth": true,
    "JwtSecret": "{JWT_SECRET}",
    "JwtIssuer": "my-app",
    "JwtAudience": "my-api",
    "JwtExpireMinutes": 60,
    "JwtRefreshExpireDays": 7,
    "JwtValidateIssuer": true,
    "JwtValidateAudience": true
  }
}

Combined Authentication

All three authentication schemes can be used together:

json
{
  "Auth": {
    "CookieAuth": true,
    "CookieValidDays": 14,
    "CookieHttpOnly": true,

    "BearerTokenAuth": true,
    "BearerTokenExpireHours": 1,

    "JwtAuth": true,
    "JwtSecret": "{JWT_SECRET}",
    "JwtExpireMinutes": 60
  }
}

Next Steps

Comments

Released under the MIT License.