Table of Contents

Class JwtHelper

Namespace
NuvTools.Security.Helpers
Assembly
NuvTools.Security.dll

Provides helper methods for generating, parsing, and validating JSON Web Tokens (JWTs).

public static class JwtHelper
Inheritance
JwtHelper
Inherited Members

Methods

Generate(string, string, string, IEnumerable<Claim>, DateTime?)

Generates a signed JWT token.

public static string Generate(string key, string issuer, string audience, IEnumerable<Claim> claims, DateTime? expires = null)

Parameters

key string

The secret key used for signing the token.

issuer string

The token issuer (usually your API or authentication service).

audience string

The intended audience for the token.

claims IEnumerable<Claim>

The claims to include in the token payload.

expires DateTime?

The optional expiration date and time for the token.

Returns

string

A signed JWT as a string.

GenerateRefreshToken()

Generates a secure random refresh token as a Base64 string.

public static string GenerateRefreshToken()

Returns

string

GetPrincipalFromExpiredToken(string, string)

Extracts the ClaimsPrincipal from an expired JWT without validating its expiration.

public static ClaimsPrincipal GetPrincipalFromExpiredToken(string token, string key)

Parameters

token string

The expired JWT.

key string

The signing key used to validate the token signature.

Returns

ClaimsPrincipal

A ClaimsPrincipal representing the token subject.

Exceptions

SecurityTokenException

Thrown if the token signature or algorithm is invalid.

IsTokenExpired(string)

Determines whether a JWT has expired based on its "exp" claim.

public static bool IsTokenExpired(string jwt)

Parameters

jwt string

Returns

bool

ParseClaimsFromJwt(string)

Parses claims directly from a JWT string without signature validation.

public static IReadOnlyList<Claim> ParseClaimsFromJwt(string jwt)

Parameters

jwt string

The JWT string.

Returns

IReadOnlyList<Claim>

A list of Claim parsed from the token payload.