/// <summary> /// Gets the DateTime using the number of seconds from 1970-01-01T0:0:0Z (UTC) /// </summary> /// <param name="key">Claim in the payload that should map to an integer.</param> /// <remarks>If the claim is not found, the function returns: DateTime.MinValue</remarks> /// <exception cref="T:System.IdentityModel.Tokens.SecurityTokenException">if an overflow exception is thrown by the runtime.</exception> /// <returns>the DateTime representation of a claim.</returns> private DateTime GetDateTime(string key) { object obj; if (!this.TryGetValue(key, out obj)) { return(DateTime.MinValue); } try { IList <object> objectList = obj as IList <object>; if (objectList != null) { if (objectList.Count == 0) { return(DateTime.MinValue); } obj = objectList[0]; } return(EpochTime.DateTime(Convert.ToInt64(obj, (IFormatProvider)CultureInfo.InvariantCulture))); } catch (Exception ex) { if (ex is FormatException || ex is ArgumentException || ex is InvalidCastException) { throw new Exception(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "IDX10700: Error found while parsing date time. The '{0}' claim has value '{1}' which is could not be parsed to an integer.\nInnerException: '{2}'.", (object)key, obj ?? (object)"<null>", (object)ex)); } if (ex is OverflowException) { throw new Exception(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "IDX10701: Error found while parsing date time. The '{0}' claim has value '{1}' does not lie in the valid range. \nInnerException: '{2}'.", (object)key, obj ?? (object)"<null>", (object)ex)); } throw; } }
/// <summary> /// Initializes a new instance of the <see cref="T:System.IdentityModel.Tokens.JwtPayload" /> class with claims added for each parameter specified. Default string comparer <see cref="P:System.StringComparer.Ordinal" />. /// </summary> /// <param name="issuer">if this value is not null, a { iss, 'issuer' } claim will be added.</param> /// <param name="audience">if this value is not null, a { aud, 'audience' } claim will be added</param> /// <param name="claims">if this value is not null then for each <see cref="T:System.Security.Claims.Claim" /> a { 'Claim.Type', 'Claim.Value' } is added. If duplicate claims are found then a { 'Claim.Type', List<object> } will be created to contain the duplicate values.</param> /// <param name="notBefore">if notbefore.HasValue is 'true' a { nbf, 'value' } claim is added.</param> /// <param name="expires">if expires.HasValue is 'true' a { exp, 'value' } claim is added.</param> /// <remarks>Comparison is set to <see cref="P:System.StringComparer.Ordinal" /> /// <para>The 4 parameters: 'issuer', 'audience', 'notBefore', 'expires' take precednece over <see cref="T:System.Security.Claims.Claim" />(s) in 'claims'. The values in 'claims' will be overridden.</para></remarks> /// <exception cref="T:System.ArgumentException">if 'expires' <= 'notbefore'.</exception> public JwtPayload( string issuer, string audience, IEnumerable <Claim> claims, DateTime?notBefore, DateTime?expires) : base((IEqualityComparer <string>)StringComparer.Ordinal) { if (expires.HasValue && notBefore.HasValue) { DateTime?nullable1 = notBefore; DateTime?nullable2 = expires; if ((nullable1.HasValue & nullable2.HasValue ? (nullable1.GetValueOrDefault() >= nullable2.GetValueOrDefault() ? 1 : 0) : 0) != 0) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "IDX10401: Expires: '{0}' must be after NotBefore: '{1}'.", (object)expires.Value, (object)notBefore.Value)); } } if (claims != null) { this.AddClaims(claims); } if (!string.IsNullOrWhiteSpace(issuer)) { this["iss"] = (object)issuer; } if (!string.IsNullOrWhiteSpace(audience)) { this["aud"] = (object)audience; } if (expires.HasValue) { this["exp"] = (object)EpochTime.GetIntDate(expires.Value.ToUniversalTime()); } if (!notBefore.HasValue) { return; } this["nbf"] = (object)EpochTime.GetIntDate(notBefore.Value.ToUniversalTime()); }