/// <summary> /// Initializes a new instance of the <see cref="JwtPayload"/> class with claims added for each parameter specified. Default string comparer <see cref="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="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="lifetime">if this value is not null, then if <para><see cref="Lifetime" />.Created.HasValue a { nbf, 'value' } is added.</para><para>if <see cref="Lifetime"/>.Expires.HasValue a { exp, 'value' } claim is added.</para></param> /// <remarks>Comparison is set to <see cref="StringComparer.Ordinal"/></remarks> public JwtPayload(string issuer = null, string audience = null, IEnumerable <Claim> claims = null, Lifetime lifetime = null) : base(StringComparer.Ordinal) { if (null != issuer) { this.Add(JwtConstants.ReservedClaims.Issuer, issuer); } if (null != audience) { this.Add(JwtConstants.ReservedClaims.Audience, audience); } if (lifetime != null) { if (lifetime.Created.HasValue) { this.Add(JwtConstants.ReservedClaims.NotBefore, EpochTime.GetIntDate(lifetime.Created.Value)); } if (lifetime.Expires.HasValue) { this.Add(JwtConstants.ReservedClaims.ExpirationTime, EpochTime.GetIntDate(lifetime.Expires.Value)); } } if (claims != null) { this.AddClaims(claims); } }
/// <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="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 dateValue; if (!this.TryGetValue(key, out dateValue)) { return(DateTime.MinValue); } // if there are multiple dates, take the first one. try { long secondsAfterBaseTime; IList <object> dateValues = dateValue as IList <object>; if (dateValues != null) { if (dateValues.Count == 0) { return(DateTime.MinValue); } else { dateValue = dateValues[0]; } } // null converts to 0. secondsAfterBaseTime = Convert.ToInt64(dateValue, CultureInfo.InvariantCulture); return(EpochTime.DateTime(secondsAfterBaseTime)); } catch (Exception ex) { if (ex is FormatException || ex is ArgumentException || ex is InvalidCastException) { throw new SecurityTokenException(string.Format(CultureInfo.InvariantCulture, JwtErrors.Jwt10100, key, dateValue ?? "<null>", ex)); } if (ex is OverflowException) { throw new SecurityTokenException(string.Format(CultureInfo.InvariantCulture, JwtErrors.Jwt10101, key, dateValue ?? "<null>", ex)); } throw; } }
/// <summary> /// Initializes a new instance of the <see cref="JwtPayload"/> class with claims added for each parameter specified. Default string comparer <see cref="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="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="StringComparer.Ordinal"/> /// <para>The 4 parameters: 'issuer', 'audience', 'notBefore', 'expires' take precednece over <see cref="Claim"/>(s) in 'claims'. The values in 'claims' will be overridden.</para></remarks> /// <exception cref="ArgumentException">if 'expires' <= 'notbefore'.</exception> public JwtPayload(string issuer, string audience, IEnumerable <Claim> claims, DateTime?notBefore, DateTime?expires) : base(StringComparer.Ordinal) { if (expires.HasValue && notBefore.HasValue) { if (notBefore >= expires) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10401, expires.Value, notBefore.Value)); } } if (claims != null) { this.AddClaims(claims); } if (!string.IsNullOrWhiteSpace(issuer)) { this[JwtRegisteredClaimNames.Iss] = issuer; } if (!string.IsNullOrWhiteSpace(audience)) { this[JwtRegisteredClaimNames.Aud] = audience; } if (expires.HasValue) { this[JwtRegisteredClaimNames.Exp] = EpochTime.GetIntDate(expires.Value.ToUniversalTime()); } if (notBefore.HasValue) { this[JwtRegisteredClaimNames.Nbf] = EpochTime.GetIntDate(notBefore.Value.ToUniversalTime()); } }