6.69JwtTokenUtility Parsing JWT Token
JwtTokenUtility is a lightweight JWT utility class that can parse the Payload portion of a JWT and extract common claims (Claims) without depending on an external JWT library. It is suitable for scenarios that require quickly obtaining information such as expiration time, issuer, and subject from a JWT.
Parsing the JWT Payload
Using the JwtTokenUtility.Parse method, you can parse a complete JWT string or a standalone Payload fragment, returning a JwtPayload instance:
var jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiZXhwIjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";var jwtPayload = JwtTokenUtility.Parse(jwt);// Get the expiration time (UTC)var exp = jwtPayload.GetExpirationTimeUtc();Internally, the Parse method automatically handles standard Base64Url encoding and pads = as needed, requiring no manual processing.
Reading Standard Claims
JwtPayload provides complete methods for reading standard JWT claims:
var jwtPayload = JwtTokenUtility.Parse(jwt);string? issuer = jwtPayload.GetIssuer(); // issstring? subject = jwtPayload.GetSubject(); // substring? audience = jwtPayload.GetAudience(); // audlong? expiration = jwtPayload.GetExpiration(); // exp (Unix seconds)long? issuedAt = jwtPayload.GetIssuedAt(); // iat (Unix seconds)long? notBefore = jwtPayload.GetNotBefore(); // nbf (Unix seconds)string? jwtId = jwtPayload.GetJwtId(); // jti// More convenience methods...Checking Whether the JWT Is Expired or Valid
// Whether it has expiredbool expired = jwtPayload.IsExpired();// Whether it is currently valid (already effective and not expired)bool active = jwtPayload.IsActive();Reading Custom Claims
In addition to standard claims, JwtPayload also supports reading the value of any custom claim:
var jwtPayload = JwtTokenUtility.Parse(jwt);// Read a string claimstring? name = jwtPayload.GetString("name");// Read an integer claimint? age = jwtPayload.GetInt32("age");long? timestamp = jwtPayload.GetInt64("timestamp");// Check whether a claim existsbool hasEmail = jwtPayload.Contains("email");Getting the Raw JSON String
The JwtPayload object exposes the raw JSON string, making custom parsing convenient:
var jwtPayload = JwtTokenUtility.Parse(jwt);string rawJson = jwtPayload.RawJson;Using It Together with Access Token Management
This utility is often used in FurionAccessTokenProvider to parse the expiration time from the refresh token returned by the server and update the ExpiresAt of HttpAccessToken:
// In the post-response callback of the Configure methodhttpRequestBuilder.SetOnPostReceiveResponse(httpResponseMessage =>{ var newRefreshToken = httpResponseMessage.Headers.GetValues("x-access-token").FirstOrDefault(); if (!string.IsNullOrWhiteSpace(newRefreshToken)) { httpAccessToken.ExpiresAt = JwtTokenUtility.Parse(newRefreshToken).GetExpirationTimeUtc()!.Value; }});With JwtTokenUtility, you can easily parse JWTs, extract claims, and perform validity checks without introducing a heavy third-party JWT library, making it especially suitable for use on the client side or in lightweight SDKs.