NumericDateExtensions Class
Reads and writes the NumericDate members of a JWT (RFC 7519 section 2): seconds since the epoch.
public static class NumericDateExtensionsInheritance System.Object → NumericDateExtensions
Methods
NumericDateExtensions.GetUnixTimeSeconds(this JsonObject, string) Method
Retrieves a System.DateTimeOffset value from a System.Text.Json.Nodes.JsonObject based on a property stored as Unix time seconds.
public static System.Nullable<System.DateTimeOffset> GetUnixTimeSeconds(this System.Text.Json.Nodes.JsonObject json, string name);Parameters
json System.Text.Json.Nodes.JsonObject
The System.Text.Json.Nodes.JsonObject from which to retrieve the date/time value.
name System.String
The property name containing the Unix time seconds.
Returns
System.Nullable<System.DateTimeOffset>
A nullable System.DateTimeOffset representing the date and time of the specified property,
or null if the property is not present.
Exceptions
System.InvalidOperationException
The value is not a number.
System.Text.Json.JsonException
The value is a JSON kind no number can be read from.
System.ArgumentOutOfRangeException
The number is outside the range a date can hold.
Remarks
Unix time seconds are widely used for representing date and time in JSON objects, especially in JWTs. This method simplifies retrieving such values by converting them directly to System.DateTimeOffset. A value that is present and cannot be read THROWS rather than answering null: a caller judging a token somebody else wrote reads through TryReadTimestamp(string, Nullable<DateTimeOffset>, string) instead, which names the claim in a refusal.
NumericDateExtensions.SetUnixTimeSeconds(this JsonObject, string, Nullable<DateTimeOffset>) Method
Sets a System.DateTimeOffset value in a System.Text.Json.Nodes.JsonObject, stored as Unix time seconds.
public static void SetUnixTimeSeconds(this System.Text.Json.Nodes.JsonObject json, string name, System.Nullable<System.DateTimeOffset> value);Parameters
json System.Text.Json.Nodes.JsonObject
The System.Text.Json.Nodes.JsonObject to modify.
name System.String
The property name under which to store the Unix time seconds.
value System.Nullable<System.DateTimeOffset>
The System.DateTimeOffset value to set. If null,
the property will be removed from the JSON object.
Remarks
Storing dates as Unix time seconds is a common practice in JWTs and other JSON structures. This method facilitates setting such values by converting System.DateTimeOffset to Unix time seconds.