Class JwtHelper
Serves as an abstract base class for providing helper functionality related to JSON Web Token (JWT) operations.
public abstract class JwtHelper
- Inheritance
-
JwtHelper
- Inherited Members
- Extension Methods
Remarks
This class is intended to be inherited by concrete implementations that define specific JWT processing behaviors.
Methods
GenerateRefreshToken()
Generates a cryptographically secure refresh token by producing 64 random bytes using RandomNumberGenerator and returning the value as a Base64-encoded string.
public static string GenerateRefreshToken()
Returns
- string
A Base64-encoded string representation of a 64-byte cryptographically random sequence suitable for use as a refresh token.
GetJwtToken(string, string, string, string, int, Claim[]?)
Creates a new JWT security token for the specified user, signing it with the provided secret using HMAC-SHA256. The token includes standard subject, name, name identifier, and unique token identifier (JTI) claims, and optionally merges any additional claims supplied.
public static JwtSecurityToken GetJwtToken(string username, string secret, string issuer, string audience, int expiration, Claim[]? additionalClaims = null)
Parameters
usernamestringThe subject identifier used to populate the token's Sub, Name, and NameIdentifier claims.
secretstringThe symmetric secret used to derive the signing key for the token.
issuerstringThe issuer (iss claim) to associate with the token.
audiencestringThe audience (aud claim) to associate with the token.
expirationintThe token lifetime in minutes, added to the current UTC time to compute the expiration date.
additionalClaimsClaim[]Optional extra claims to merge into the token alongside the standard claims. If null, only the default claims are included.
Returns
- JwtSecurityToken
A JwtSecurityToken signed with HMAC-SHA256 and configured with the supplied issuer, audience, expiration, and claims.
GetUsernameFromPrincipal(ClaimsPrincipal)
return username from claim
public static string? GetUsernameFromPrincipal(ClaimsPrincipal principal)
Parameters
principalClaimsPrincipal