Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SymmetricSignatureProvider"/> class that uses an <see cref="SymmetricSecurityKey"/> to create and / or verify signatures over a array of bytes.
        /// </summary>
        /// <param name="key">The <see cref="SymmetricSecurityKey"/> used for signing.</param>
        /// <param name="algorithm">The signature algorithm to use.</param>
        /// <exception cref="ArgumentNullException">'key' is null.</exception>
        /// <exception cref="ArgumentNullException">'algorithm' is null.</exception>
        /// <exception cref="ArgumentException">'algorithm' contains only whitespace.</exception>
        /// <exception cref="ArgumentOutOfRangeException">'<see cref="SymmetricSecurityKey"/>.KeySize' is smaller than <see cref="SignatureProviderFactory.MinimumSymmetricKeySizeInBits"/>.</exception>
        /// <exception cref="InvalidOperationException"><see cref="SymmetricSecurityKey.GetKeyedHashAlgorithm"/> throws.</exception>
        /// <exception cref="InvalidOperationException"><see cref="SymmetricSecurityKey.GetKeyedHashAlgorithm"/> returns null.</exception>
        /// <exception cref="InvalidOperationException"><see cref="SymmetricSecurityKey.GetSymmetricKey"/> throws.</exception>
        public SymmetricSignatureProvider(SymmetricSecurityKey key, string algorithm)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (null == algorithm)
            {
                throw new ArgumentNullException(algorithm);
            }

            if (string.IsNullOrWhiteSpace(algorithm))
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10002, "algorithm"));
            }

            if (key.KeySize < SignatureProviderFactory.MinimumSymmetricKeySizeInBits)
            {
                throw new ArgumentOutOfRangeException("key.KeySize", key.KeySize, string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10603, key.GetType(), SignatureProviderFactory.MinimumSymmetricKeySizeInBits));
            }

            try
            {
                this.keyedHash = key.GetKeyedHashAlgorithm(algorithm);
            }
            catch (Exception ex)
            {
                if (DiagnosticUtility.IsFatal(ex))
                {
                    throw;
                }

                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10632, algorithm, key, ex), ex);
            }

            if (this.keyedHash == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10633, algorithm, key));
            }

            try
            {
                this.keyedHash.Key = key.GetSymmetricKey();
            }
            catch (Exception ex)
            {
                if (DiagnosticUtility.IsFatal(ex))
                {
                    throw;
                }

                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10634, algorithm, key, ex), ex);
            }
        }
 public SessionSecurityToken(ClaimsPrincipal claimsPrincipal, UniqueId contextId, string context, string endpointId, DateTime?validFrom, DateTime?validTo, SymmetricSecurityKey key)
 {
     ClaimsPrincipal = claimsPrincipal;
     ContextId       = contextId;
     Context         = context;
     EndpointId      = endpointId;
     validFrom       = (validFrom.HasValue) ? validFrom.Value.ToUniversalTime() : DateTime.UtcNow;
     validTo         = (validTo.HasValue) ? validTo.Value.ToUniversalTime() : ValidFrom + SessionSecurityTokenHandler.DefaultTokenLifetime;
     securityKeys    = new ReadOnlyCollection <SecurityKey> (new SecurityKey[] { new InMemorySymmetricSecurityKey((key == null) ? null : key.GetSymmetricKey()) });
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="SymmetricSignatureProvider"/> class that uses an <see cref="SymmetricSecurityKey"/> to create and / or verify signatures over a array of bytes.
        /// </summary>
        /// <param name="key">The <see cref="SymmetricSecurityKey"/> used for signing.</param>
        /// <param name="algorithm">The signature algorithm to use.</param>
        /// <exception cref="ArgumentNullException">'key' is null.</exception>
        /// <exception cref="ArgumentNullException">'algorithm' is null.</exception>
        /// <exception cref="ArgumentException">'algorithm' contains only whitespace.</exception>
        /// <exception cref="ArgumentOutOfRangeException">'<see cref="SymmetricSecurityKey"/>.KeySize' is smaller than <see cref="SignatureProviderFactory.MinimumSymmetricKeySizeInBits"/>.</exception>
        /// <exception cref="InvalidOperationException"><see cref="SymmetricSecurityKey.GetKeyedHashAlgorithm"/> throws.</exception>
        /// <exception cref="InvalidOperationException"><see cref="SymmetricSecurityKey.GetKeyedHashAlgorithm"/> returns null.</exception>
        /// <exception cref="InvalidOperationException"><see cref="SymmetricSecurityKey.GetSymmetricKey"/> throws.</exception>
        public SymmetricSignatureProvider(SymmetricSecurityKey key, string algorithm)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (null == algorithm)
            {
                throw new ArgumentNullException(algorithm);
            }

            if (string.IsNullOrWhiteSpace(algorithm))
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10002, "algorithm"));
            }

            if (key.KeySize < SignatureProviderFactory.MinimumSymmetricKeySizeInBits)
            {
                throw new ArgumentOutOfRangeException("key.KeySize", key.KeySize, string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10603, key.GetType(), SignatureProviderFactory.MinimumSymmetricKeySizeInBits));
            }

            try
            {
                this.keyedHash = key.GetKeyedHashAlgorithm(algorithm);
            }
            catch (Exception ex)
            {
                if (DiagnosticUtility.IsFatal(ex))
                {
                    throw;
                }

                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10632, algorithm, key, ex), ex);
            }

            if (this.keyedHash == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10633, algorithm, key));
            }

            try
            {
                this.keyedHash.Key = key.GetSymmetricKey();
            }
            catch (Exception ex)
            {
                if (DiagnosticUtility.IsFatal(ex))
                {
                    throw;
                }

                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10634, algorithm, key, ex), ex);
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SessionSecurityToken"/> class.
 /// </summary>
 /// <param name="claimsPrincipal"><see cref="ClaimsPrincipal"/> associated with this session.</param>
 /// <param name="contextId">Context Identifier that identifies the session</param>
 /// <param name="context">Optional context information associated with the session.</param>
 /// <param name="endpointId">The endpoint to which this token is bound. String.Empty would create a unscoped token.</param>
 /// <param name="validFrom">DateTime specifying the time the token becomes valid.</param>
 /// <param name="validTo">DateTime specifying the time the token becomes invalid.</param>
 /// <param name="key">Optional symmetric session key.</param>
 /// <exception cref="ArgumentNullException">The input parameter 'claimsPrincipal' is null.</exception>
 /// <exception cref="ArgumentNullException">The input parameter 'contextId' is null.</exception>
 /// <exception cref="ArgumentOutOfRangeException">validFrom is greater than or equal to validTo.</exception>
 /// <exception cref="ArgumentOutOfRangeException">validTo is less than current time.</exception>
 /// <remarks>
 /// If no key is supplied, a 128bit key is generated. KeyEffectiveTime is set to validFrom, KeyExpirationTime is set to validTo.
 /// A key generation identifier is created.
 /// </remarks>
 public SessionSecurityToken(ClaimsPrincipal claimsPrincipal,
                              SysUniqueId contextId,
                              string context,
                              string endpointId,
                              DateTime? validFrom,
                              DateTime? validTo,
                              SymmetricSecurityKey key)
     : this(claimsPrincipal, contextId, System.IdentityModel.UniqueId.CreateUniqueId(), context, key == null ? null : key.GetSymmetricKey(), endpointId, validFrom, validTo, null, validFrom, validTo, null, null)
 {
 }
Ejemplo n.º 5
0
		public SessionSecurityToken (ClaimsPrincipal claimsPrincipal, UniqueId contextId, string context, string endpointId, DateTime? validFrom, DateTime? validTo, SymmetricSecurityKey key) {
			ClaimsPrincipal = claimsPrincipal;
			ContextId = contextId;
			Context = context;
			EndpointId = endpointId;
			validFrom = (validFrom.HasValue) ? validFrom.Value.ToUniversalTime () : DateTime.UtcNow;
			validTo = (validTo.HasValue) ? validTo.Value.ToUniversalTime () : ValidFrom + SessionSecurityTokenHandler.DefaultTokenLifetime;
			securityKeys = new ReadOnlyCollection<SecurityKey> (new SecurityKey[] { new InMemorySymmetricSecurityKey ((key == null) ? null : key.GetSymmetricKey ()) });
		}