How to retrieve all certificates in your X509Store

对着背影说爱祢 提交于 2019-11-30 10:43:50

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();
cealex

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();

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!