當前位置: 首頁>>代碼示例>>C#>>正文


C# X509Store.Dispose方法代碼示例

本文整理匯總了C#中System.Security.Cryptography.X509Certificates.X509Store.Dispose方法的典型用法代碼示例。如果您正苦於以下問題:C# X509Store.Dispose方法的具體用法?C# X509Store.Dispose怎麽用?C# X509Store.Dispose使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Security.Cryptography.X509Certificates.X509Store的用法示例。


在下文中一共展示了X509Store.Dispose方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: X509SecurityTokenProvider

        public X509SecurityTokenProvider(StoreLocation storeLocation, StoreName storeName, X509FindType findType, object findValue)
        {
            if (findValue == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("findValue");
            }

            X509Store store = new X509Store(storeName, storeLocation);
            X509Certificate2Collection certificates = null;
            try
            {
                store.Open(OpenFlags.ReadOnly);
                certificates = store.Certificates.Find(findType, findValue, false);
                if (certificates.Count < 1)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.CannotFindCert, storeName, storeLocation, findType, findValue)));
                }
                if (certificates.Count > 1)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.FoundMultipleCerts, storeName, storeLocation, findType, findValue)));
                }

                _certificate = new X509Certificate2(certificates[0].Handle);
            }
            finally
            {
                System.ServiceModel.Security.SecurityUtils.ResetAllCertificates(certificates);
                store.Dispose();
            }
        }
開發者ID:SoumikMukherjeeDOTNET,項目名稱:wcf,代碼行數:30,代碼來源:X509SecurityTokenProvider.cs

示例2: FindCertificateByThumbprint

        /// <summary>
        /// Finds the cert having thumbprint supplied from store location supplied
        /// </summary>
        /// <param name="storeName"></param>
        /// <param name="storeLocation"></param>
        /// <param name="thumbprint"></param>
        /// <param name="validationRequired"></param>
        /// <returns>X509Certificate2</returns>
        public static X509Certificate2 FindCertificateByThumbprint(StoreName storeName, StoreLocation storeLocation, string thumbprint, bool validationRequired)
        {
            Guard.ArgumentNotNullOrWhiteSpace(thumbprint, nameof(thumbprint));

            var store = new X509Store(storeName, storeLocation);
            try
            {
                store.Open(OpenFlags.ReadOnly);
                var col = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, validationRequired);
                if (col == null || col.Count == 0)
                {
                    throw new ArgumentException("certificate was not found in store");
                }

                return col[0];
            }
            finally
            {
#if NET451
                // IDisposable not implemented in NET451
                store.Close();
#else
                // Close is private in DNXCORE, but Dispose calls close internally
                store.Dispose();
#endif
            }
        }
開發者ID:mspnp,項目名稱:multitenant-saas-guidance,代碼行數:35,代碼來源:CertificateUtility.cs

示例3: GetCertificate

        internal static X509Certificate2 GetCertificate(StoreName name, StoreLocation location, string thumbprint) {
            var store = new X509Store(name, location);

            try {
                store.Open(OpenFlags.ReadOnly);

                var certificates = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, validOnly: false);

                return certificates.OfType<X509Certificate2>().SingleOrDefault();
            }

            finally {
#if DNXCORE50
                store.Dispose();
#else
                store.Close();
#endif
            }
        }
開發者ID:Fosol,項目名稱:Example.Oauth,代碼行數:19,代碼來源:OpenIdConnectServerHelpers.cs

示例4: GetCertificate

        /// <summary>
        /// Searches the stores for certificate with subject name matching the host and path extracted from the applicationUri.
        /// </summary>
        /// <param name="description">The <see cref="ApplicationDescription"/>.</param>
        /// <param name="createIfNotFound">Creates a new self-signed certificate if one not found.</param>
        /// <returns>The certificate. </returns>
        public static X509Certificate2 GetCertificate(this ApplicationDescription description, bool createIfNotFound = true)
        {
            if (description == null)
            {
                throw new ArgumentNullException(nameof(description));
            }

            if (string.IsNullOrEmpty(description.ApplicationUri))
            {
                throw new ArgumentOutOfRangeException(nameof(description), "Expecting ApplicationUri in the form of 'http://{hostname}/{appname}'.");
            }

            string subjectName = null;

            UriBuilder appUri = new UriBuilder(description.ApplicationUri);
            if (appUri.Scheme == "http" && !string.IsNullOrEmpty(appUri.Host))
            {
                var path = appUri.Path.Trim('/');
                if (!string.IsNullOrEmpty(path))
                {
                    subjectName = $"CN={path}, DC={appUri.Host}";
                }
            }

            if (appUri.Scheme == "urn")
            {
                var parts = appUri.Path.Split(new[] { ':' }, 2);
                if (parts.Length == 2)
                {
                    subjectName = $"CN={parts[1]}, DC={parts[0]}";
                }
            }

            if (subjectName == null)
            {
                throw new ArgumentOutOfRangeException(nameof(description), "Expecting ApplicationUri in the form of 'http://{hostname}/{appname}' -or- 'urn:{hostname}:{appname}'.");
            }

            X509Certificate2 clientCertificate = null;
            X509Store store = null;
            List<X509Certificate2> foundCerts = new List<X509Certificate2>();

            // First check the Local Machine store.
            store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            try
            {
                store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                var certs = store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, subjectName, false);
                if (certs.Count > 0)
                {
                    foundCerts.AddRange(certs.OfType<X509Certificate2>());
                }
            }
            catch (Exception ex)
            {
                Log.Warn($"Error opening X509Store '{store}'. {ex.Message}");
            }
            finally
            {
                store.Dispose();
            }

            // Then check the Current User store.
            store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            try
            {
                store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                var certs = store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, subjectName, false);
                if (certs.Count > 0)
                {
                    foundCerts.AddRange(certs.OfType<X509Certificate2>());
                }
            }
            catch (Exception ex)
            {
                Log.Warn($"Error opening X509Store '{store}'. {ex.Message}");
            }
            finally
            {
                store.Dispose();
            }

            // Select the certificate that was created last.
            if (foundCerts.Count > 0)
            {
                clientCertificate = foundCerts.OrderBy(c => c.NotBefore).Last();
                Log.Info($"Found certificate '{subjectName}'.");
                return clientCertificate;
            }

            Log.Info($"Creating new certificate '{subjectName}'.");
            try
            {
                var pfx = CertificateGenerator.CreateSelfSignCertificatePfx(
//.........這裏部分代碼省略.........
開發者ID:yuriik83,項目名稱:workstation-uaclient,代碼行數:101,代碼來源:X509CertificateExtensions.cs

示例5: StoreContainsCertificate

 static bool StoreContainsCertificate(StoreName storeName, X509Certificate2 certificate)
 {
     X509Store store = new X509Store(storeName, StoreLocation.CurrentUser);
     X509Certificate2Collection certificates = null;
     try
     {
         store.Open(OpenFlags.ReadOnly);
         certificates = store.Certificates.Find(X509FindType.FindByThumbprint, certificate.Thumbprint, false);
         return certificates.Count > 0;
     }
     finally
     {
         SecurityUtils.ResetAllCertificates(certificates);
         store.Dispose();
     }
 }
開發者ID:KKhurin,項目名稱:wcf,代碼行數:16,代碼來源:X509CertificateValidator.cs

示例6: GetCertificate

        static X509Certificate2 GetCertificate(string certFindValue)
        {
            StoreLocation[] locations = new StoreLocation[] { StoreLocation.LocalMachine, StoreLocation.CurrentUser };
            foreach (StoreLocation location in locations)
            {
                X509Store store = new X509Store(StoreName.My, location);
                store.Open(OpenFlags.OpenExistingOnly);

                X509Certificate2Collection collection = store.Certificates.Find(
                    X509FindType.FindBySubjectName,
                    certFindValue,
                    false);

                if (collection.Count == 0)
                {
                    collection = store.Certificates.Find(
                        X509FindType.FindByThumbprint,
                        certFindValue,
                        false);
                }

#if DOTNET_CORE
                store.Dispose();
#else
                store.Close();
#endif
                if (collection.Count > 0)
                {
                    return collection[0];
                }
            }

            throw new ArgumentException("No certificate can be found using the find value.");            
        }
開發者ID:Azure,項目名稱:azure-amqp,代碼行數:34,代碼來源:AmqpUtils.cs

示例7: GetCertificate

        static X509Certificate2 GetCertificate(StoreLocation storeLocation, StoreName storeName, string certFindValue)
        {
            X509Store store = new X509Store(storeName, storeLocation);
            store.Open(OpenFlags.OpenExistingOnly);
            X509Certificate2Collection collection = store.Certificates.Find(
                X509FindType.FindBySubjectName,
                certFindValue,
                false);
            if (collection.Count == 0)
            {
                throw new ArgumentException("No certificate can be found using the find value " + certFindValue);
            }

#if DOTNET
            store.Dispose();
#else
            store.Close();
#endif
            return collection[0];
        }
開發者ID:mbroadst,項目名稱:amqpnetlite,代碼行數:20,代碼來源:ContainerHostTests.cs

示例8: GetX509Certificate

        /// <summary>
        /// Get X509 certificate from the certificate store.
        /// </summary>
        /// <param name="certificateName">Certificate name.</param>
        /// <returns>Certificate with the specified name.</returns>
        private static X509Certificate GetX509Certificate(string certificateName)
        {
            var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

            store.Open(OpenFlags.ReadOnly);
            var certs = store.Certificates.Find(X509FindType.FindBySubjectName, certificateName, false);
#if NETSTANDARD
            store.Dispose();
#else
            store.Close();
#endif

            if (certs.Count == 0)
            {
                throw new DicomNetworkException("Unable to find certificate for " + certificateName);
            }

            return certs[0];
        }
開發者ID:aerik,項目名稱:fo-dicom,代碼行數:24,代碼來源:DesktopNetworkListener.cs

示例9: AddToStoreIfNeeded

        // Adds the given certificate to the given store unless it is
        // already present.  Returns 'true' if the certificate was added.
        private static bool AddToStoreIfNeeded(StoreName storeName,
                                               StoreLocation storeLocation,
                                               X509Certificate2 certificate)
        {
            X509Store store = null;
            X509Certificate2 existingCert = null;
            lock(s_certificateLock)
            {
                try
                {
                    store = new X509Store(storeName, storeLocation);
                    store.Open(OpenFlags.ReadWrite);
                    existingCert = CertificateFromThumbprint(store, certificate.Thumbprint);
                    if (existingCert == null)
                    {
                        store.Add(certificate);
                    }
                }
                finally
                {
                    if (store != null)
                    {
                        store.Dispose();
                    }
                }

                return existingCert == null;
            }
        }
開發者ID:htlp,項目名稱:wcf,代碼行數:31,代碼來源:BridgeClientCertificateManager.cs


注:本文中的System.Security.Cryptography.X509Certificates.X509Store.Dispose方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。