Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new <c>VssSigningCredentials</c> instance using the specified <paramref name="factory"/>
        /// callback function to retrieve the signing key.
        /// </summary>
        /// <param name="factory">The factory which creates <c>RSA</c> keys used for signing and verification</param>
        /// <returns>A new <c>VssSigningCredentials</c> instance which uses the specified provider for signing</returns>
        public static VssSigningCredentials Create(Func <RSA> factory)
        {
            ArgumentUtility.CheckForNull(factory, nameof(factory));

            using (var rsa = factory())
            {
                if (rsa == null)
                {
                    throw new InvalidCredentialsException(JwtResources.SignatureAlgorithmUnsupportedException("None"));
                }

                if (rsa.KeySize < c_minKeySize)
                {
                    throw new InvalidCredentialsException(JwtResources.SigningTokenKeyTooSmall());
                }

                return(new RSASigningToken(factory, rsa.KeySize));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new <c>VssSigningCredentials</c> instance using the specified <paramref name="certificate"/> instance
        /// as the signing key.
        /// </summary>
        /// <param name="certificate">The certificate which contains the key used for signing and verification</param>
        /// <returns>A new <c>VssSigningCredentials</c> instance which uses the specified certificate for signing</returns>
        public static VssSigningCredentials Create(X509Certificate2 certificate)
        {
            ArgumentUtility.CheckForNull(certificate, nameof(certificate));

            if (certificate.HasPrivateKey)
            {
                var rsa = certificate.GetRSAPrivateKey();
                if (rsa == null)
                {
                    throw new SignatureAlgorithmUnsupportedException(certificate.SignatureAlgorithm.FriendlyName);
                }

                if (rsa.KeySize < c_minKeySize)
                {
                    throw new InvalidCredentialsException(JwtResources.SigningTokenKeyTooSmall());
                }
            }

            return(new X509Certificate2SigningToken(certificate));
        }