Esempio n. 1
0
        /// <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 (!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, ErrorMessages.IDX10700, key, dateValue ?? "<null>", ex));
                }

                if (ex is OverflowException)
                {
                    throw new SecurityTokenException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10701, key, dateValue ?? "<null>", ex));
                }

                throw;
            }
        }
Esempio n. 2
0
        /// <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&lt;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' &lt;= '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());
            }
        }