How to read certificates from my certificate store?

I want to install a certificate on a machine if it is not already installed. I tried to check if the store contains a certificate, but somehow my store is always empty. I checked the "Intermediate Certificate Authorities" folder and found 18 certificates there. So why is this code writing 0?

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

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

+7
certificate
source share
2 answers

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

+8
source share

One possible explanation may be that the process executing this code may not have permissions for this particular repository.

The Cert store really is a wrapper around a special part of the file system, and all certificates are really just files. They all have access control lists (ACLs), so if you do not have rights, you cannot see them.

You can easily check if this is your problem by running the code with the priviliges administrator (remember UAC, though).

0
source share

All Articles