How to retrieve all certificates in your X509Store

后端 未结 4 662
被撕碎了的回忆
被撕碎了的回忆 2021-01-01 10:07

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 tri

相关标签:
4条回答
  • 2021-01-01 10:26

    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();
    
    0 讨论(0)
  • 2021-01-01 10:29

    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();
    
    0 讨论(0)
  • 2021-01-01 10:40

    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

    0 讨论(0)
  • 2021-01-01 10:42

    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.

    0 讨论(0)
提交回复
热议问题