Esempio n. 1
0
        /// <summary>
        /// Get the server credentials encoded from the certificate store or file.
        /// </summary>
        /// <param name="section">The config section group and section name.</param>
        /// <returns>The x509 certificate model else null.</returns>
        /// <exception cref="System.Exception">Configuration load exception is thrown.</exception>
        public X509Certificate2Model GetServerCredentialsEncoded(string section = "NequeoSecurityGroup/NequeoSecurityHost")
        {
            X509Certificate2Model certificate = null;

            try
            {
                // Refreshes the named section so the next time that it is retrieved it will be re-read from disk.
                System.Configuration.ConfigurationManager.RefreshSection(section);

                // Create a new default host type
                // an load the values from the configuration
                // file into the default host type.
                SecurityHost defaultHost =
                    (SecurityHost)System.Configuration.ConfigurationManager.GetSection(section);

                // Make sure the section is defined.
                if (defaultHost == null)
                {
                    throw new Exception("Configuration section has not been defined.");
                }

                // Get the server credetials element.
                ServerCredentialsEncodedElement serverCredentials = defaultHost.ServerCredentialsEncodedSection;
                if (serverCredentials == null)
                {
                    throw new Exception("Configuration element ServerCredentialsEncoded has not been defined.");
                }

                // Get the certificate path
                ServerCredentialsCertificatePathElement certificatePath = serverCredentials.CertificatePath;
                if (certificatePath == null)
                {
                    throw new Exception("Configuration element CertificatePath has not been defined.");
                }

                // Create the X509 certificate model.
                certificate = new X509Certificate2Model()
                {
                    UseServerCertificate = serverCredentials.UseServerCertificate,
                    Path     = certificatePath.Path,
                    Password = certificatePath.Password
                };
            }
            catch (Exception)
            {
                throw;
            }

            // Return the certificate.
            return(certificate);
        }
Esempio n. 2
0
        /// <summary>
        /// Get the server credentials from the certificate store or file.
        /// </summary>
        /// <param name="section">The config section group and section name.</param>
        /// <returns>The x509 certificate else null.</returns>
        /// <exception cref="System.Exception">Configuration load exception is thrown.</exception>
        public X509Certificate2 GetServerCredentials(string section = "NequeoSecurityGroup/NequeoSecurityHost")
        {
            X509Certificate2 certificate = null;

            try
            {
                // Refreshes the named section so the next time that it is retrieved it will be re-read from disk.
                System.Configuration.ConfigurationManager.RefreshSection(section);

                // Create a new default host type
                // an load the values from the configuration
                // file into the default host type.
                SecurityHost defaultHost =
                    (SecurityHost)System.Configuration.ConfigurationManager.GetSection(section);

                // Make sure the section is defined.
                if (defaultHost == null)
                {
                    throw new Exception("Configuration section has not been defined.");
                }

                // Get the server credetials element.
                ServerCredentialsElement serverCredentials = defaultHost.ServerCredentialsSection;
                if (serverCredentials == null)
                {
                    throw new Exception("Configuration element ServerCredentials has not been defined.");
                }

                // Should a certificate be loaded.
                if (serverCredentials.UseServerCertificate)
                {
                    // If using the certificate store.
                    if (serverCredentials.UseCertificateStore)
                    {
                        // Get the certificate from the store.
                        ServerCredentialsCertificateStoreElement certificateStore = serverCredentials.CertificateStore;
                        if (certificateStore == null)
                        {
                            throw new Exception("Configuration element CertificateStore has not been defined.");
                        }

                        // Get the certificate refrence details from the certificate store.
                        certificate = X509Certificate2Store.GetCertificate(
                            certificateStore.StoreName,
                            certificateStore.StoreLocation,
                            certificateStore.X509FindType,
                            certificateStore.FindValue,
                            false);
                    }
                    else
                    {
                        // Get the certificate path
                        ServerCredentialsCertificatePathElement certificatePath = serverCredentials.CertificatePath;
                        if (certificatePath == null)
                        {
                            throw new Exception("Configuration element CertificatePath has not been defined.");
                        }

                        // Get the certificate path details and create
                        // the x509 certificate reference.
                        certificate = X509Certificate2Store.GetCertificate(certificatePath.Path, certificatePath.Password);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

            // Return the certificate.
            return(certificate);
        }