agnieszka
agnieszka

Reputation: 15345

How to read certificates from my certificate store?

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?

Upvotes: 7

Views: 5761

Answers (2)

Pent Ploompuu
Pent Ploompuu

Reputation: 5414

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

Upvotes: 8

Mark Seemann
Mark Seemann

Reputation: 233125

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).

Upvotes: 0

Related Questions