Ejemplo n.º 1
0
        /// <summary>
        /// Finds a certificate in a store.
        /// </summary>
        /// <param name="needPrivateKey">if set to <c>true</c> the returned certificate must contain the private key.</param>
        /// <returns>An instance of the <see cref="X509Certificate2"/> that is emebeded by this instance or find it in
        /// the selected strore pointed out by the <see cref="StorePath"/> using selected <see cref="SubjectName"/>.</returns>
        public async Task <X509Certificate2> Find(bool needPrivateKey)
        {
            X509Certificate2 certificate = null;

            // check if the entire certificate has been specified.
            if (m_certificate != null)
            {
                certificate = m_certificate;
            }
            else
            {
                // open store.
                ICertificateStore store = CertificateStoreIdentifier.PickStore(StoreType);

                store.Open(StorePath);

                X509Certificate2Collection collection = await store.Enumerate();

                certificate = Find(collection, m_thumbprint, m_subjectName, needPrivateKey);

                if (certificate != null)
                {
                    m_certificate = certificate;
                }
            }

            return(certificate);
        }