balexandre
balexandre

Reputation: 75093

Understanding the retrival of Certificates on a server

I have made a simple console app that loops through all the Certificates on a Machine

private static X509Certificate2 GetSpecifiedCertificate(StoreName storeName, StoreLocation storeLocation)
{
    X509Store store = new X509Store(storeName, storeLocation);
    store.Open(OpenFlags.ReadOnly);

    X509Certificate2Collection certs = store.Certificates;


    if (certs.Count > 0)
    {
        Console.WriteLine(string.Format("found {0} certficates", certs.Count));

        for (int i = 0; i < certs.Count; i++)
        {
            X509Certificate2 cert = certs[i];
            Console.WriteLine(cert.Thumbprint);
        }
    }
    else
        Console.WriteLine("found no certficates at all");

    return null;
}

using StoreName.CertificateAuthority and StoreLocation.LocalMachine as the variables, on my Windows Server 2008R2, I only get 3 Certificates even though there are many more installed

console app output: enter image description here

installed certificates under the CertificateAuthority store location enter image description here

How do I get the missing ones?

I specially wan to retrieve the Apple Certificate one to sign files, but no matter how I install the public certificate, I can not retrieve it from a store loop...

Do I always need to restart the machine? Is there a special trick to get them?

Upvotes: 1

Views: 677

Answers (2)

Lex Li
Lex Li

Reputation: 63203

How did you open the certificate window? I think you are looking at the certificates under your account instead of the computer account. However, the code queries certificates from the computer account, which usually has less certificates installed than your account.

To open the certificates window for the computer account,

  1. Execute mmc at command prompt.
  2. File | Add/Remove Snap-in.
  3. Add Certificates.
  4. Choose Computer account.

Upvotes: 1

nover
nover

Reputation: 2369

Generally, it should work with loading the "CertificateAuthority" store and fetching it from there, so it's kinda weird.

However, according to MSDN: http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.storename.aspx

It should be possible to load the Apple certificate like this:

var appleCert = new  X509Certificate2("appleRoot.cer");

Just to get you started.

Upvotes: 0

Related Questions