Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve all certificates in your X509Store

I am using the following code to retrieve all certificates in my PC from an asp.net webapp. The certificates collection is empty, and I can't understand why.

I tried impersonating my own user account and I didn't succeed as well. What am I doing wrong?

var store = new X509Store(StoreLocation.CurrentUser); //StoreLocation.LocalMachine fails too
var certificates = store.Certificates;
foreach (var certificate in certificates)
{
    var friendlyName = certificate.FriendlyName;
    Console.WriteLine(friendlyName);
}

//original problem: fetch a single certificate by its subject name
X509Certificate2 clientCertificate = CertificateUtility.GetCertificate(StoreName.My, StoreLocation.CurrentUser,  "CN=mypc.domainname"); //returns null :(
like image 770
Pablo Avatar asked Jan 18 '11 21:01

Pablo


4 Answers

Add this line of code to the second line and see how it works:

store.Open(OpenFlags.ReadOnly); 

and then this at the bottom :):

store.Close(); 
like image 112
Chris B. Behrens Avatar answered Oct 06 '22 12:10

Chris B. Behrens


All in one ...

I have an apache server (xamp) with https. I access through https and c# (vs2010) to a PHP upload page

  1. Install the certificate from i.e in the personal folder certificate, for example.

  2. To view the certicates run "certmgr.msc" , at least in win7

Listing the personal certificates

var store = new X509Store(StoreLocation.CurrentUser);   store.Open(OpenFlags.ReadOnly);   var certificates = store.Certificates; foreach (var certificate in certificates) {     var friendlyName = certificate.FriendlyName;     var xname = certificate.GetName(); //obsolete     Console.WriteLine(friendlyName); }  store.Close(); 

Find specific certificate

string certificateName = "CN=localhost"; //name found in the var xname X509Store storex = new X509Store(StoreName.My, StoreLocation.CurrentUser);                     storex.Open(OpenFlags.ReadOnly); X509Certificate2Collection certificatesx =             storex.Certificates.Find(X509FindType.FindBySubjectName,              certificateName,             true);  X509Certificate certificatex = certificates[0];  storex.Close(); 
like image 41
cealex Avatar answered Oct 06 '22 13:10

cealex


I can find certificates by ...

var certificateStore = new X509Store(StoreName.TrustedPeople, StoreLocation.LocalMachine);

certificateStore.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

var certificateCollection = certificateStore.Certificates.Find(X509FindType.FindBySubjectName, "mycert.me.com",false);

certificateStore.Close();

var certificate = certificateCollection[0];

certificateCollection will have the certificates I care about ... if it is just one then I get first element in the collection.

like image 23
Nabheet Avatar answered Oct 06 '22 13:10

Nabheet


Look in your certificate store(mmc/add/certificate snap-in/my user account/Certificates - Current User/Personal/Certificates) to see the subject name to make sure "CN=mypc.domainname" is whats actually on the cert.

"CN=mypc.domainname"

vs

"CN = mypc.domainname"

...etc

like image 37
rick schott Avatar answered Oct 06 '22 13:10

rick schott