views:

224

answers:

2

I want to install a certificate on a machine if it doesn't have it installed already. I tried checking if the store contains the certificate but somehow my store is always empty. I checked "Intermediate Certification Authorities" folder and found 18 certificates there. So why does this code write 0?

X509Store store = new X509Store(StoreName.CertificateAuthority, StoreLocation.LocalMachine);
Console.WriteLine(store.Certificates.Count);

I also tried StoreLocation.CurrentUser. What am I doing wrong?

A: 

One possible explanation may be that the process that runs this code may not have permissions to that particular store.

The Cert store is really just a wrapper around a special part of the file system, and all the certs are really just files. They all have Access Control Lists (ACLs), so if you don't have the right permissions, you can't see them.

You can easily verify if this is your problem by running the code with Administrator priviliges (be aware of UAC, though).

Mark Seemann
+1  A: 

You have to call store.Open(OpenFlags.ReadWrite); before you can access the certificates.

Pent Ploompuu