// Protected implementation of Dispose pattern.
        protected virtual void Dispose(bool disposing)
        {
            if (m_Disposed)
            {
                return;
            }
            if (disposing)
            {
                // Free any other managed objects here.
            }

            // Free any unmanaged objects here.
            if (ClientCert != null)
            {
                ClientCert.Dispose();
                ClientCert = null;
            }
            if (ClientCaChain != null)
            {
                foreach (X509Certificate2 cert in ClientCaChain)
                {
                    cert.Dispose();
                }
                ClientCaChain = null;
            }
            m_Disposed = true;
        }
Exemplo n.º 2
0
        // GET api/certificatetest
        public CertificateSettings Get()
        {
            CertificateSettings settings   = new CertificateSettings();
            X509Store           _certStore = null;

            string _strCertificateAppSettings = string.Empty;

            try
            {
                _certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                _certStore.Open(OpenFlags.ReadOnly);

                _strCertificateAppSettings = Environment.GetEnvironmentVariable("APPSETTING_WEBSITE_LOAD_CERTIFICATES");

                if (string.IsNullOrEmpty(_strCertificateAppSettings) != true)
                {
                    settings.AppsettingWebsiteLoadCertificatesValue = _strCertificateAppSettings;

                    X509Certificate2Collection col = _certStore.Certificates;

                    if (_certStore.Certificates.Count > 0)
                    {
                        foreach (X509Certificate2 _cert in _certStore.Certificates)
                        {
                            ClientCert cert = new ClientCert
                            {
                                IssuerName     = _cert.IssuerName.Name,
                                SubjectName    = _cert.SubjectName.Name,
                                Thumbprint     = _cert.Thumbprint,
                                HasPrivateKey  = _cert.HasPrivateKey,
                                ExpirationDate = _cert.NotAfter,
                                IsExportable   = IsPrivateKeyExportable(_cert)
                            };
                            settings.Certificates.Add(cert);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                _certStore.Close();
            }
            return(settings);
        }