Get list of certificates from the certificate store in C#

Viewed 116093

For a secure application I need to select a certificate in a dialog. How can I access certificate store or a part of it (e.g. storeLocation="Local Machine" and storeName="My") using C# and get a collection of all certificates from there? Thanks in advance for your help.

5 Answers

Yes -- the X509Store.Certificates property returns a snapshot of the X.509 certificate store.

Example for the above question.

    public List<string> getListofCertificate()
    {
        var certificates = new List<string>();
        X509Store store = new X509Store(StoreLocation.CurrentUser);
        try
        {
            store.Open(OpenFlags.ReadOnly);

            // Place all certificates in an X509Certificate2Collection object.
            X509Certificate2Collection certCollection = store.Certificates;
            foreach (X509Certificate2 x509 in certCollection)
                {
                    Console.WriteLine(x509.IssuerName.Name);
                    certificates.Add(x509.IssuerName.Name);
                }
        }
        finally
        {
            store.Close();
        }
        return certificates;

    }
Related