当前位置: 首页>>代码示例>>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;未经允许,请勿转载。