Failed to get X509Certificate2 certificate by C # code

I want to find a certificate from my store, but using the following code, I could not get the certificate. It always returns null.

code to get certificate by thumbprint

What is wrong with my code?

Update:

I copied my certificate fingerprint by examining the repository object and comparing it with my fingerprint string and returning false! I think that the problem of interpreting a string in the VS2010 IDE or the problem with copy paste can be seen below in Fig. because of this, it must ignore the certificate from the list. Has anyone encountered this type of problem before?

enter image description here

+3
source share
2 answers

Well, the certificate collection is empty because there is no certificate with this fingerprint. Check:

Try:

  • mmc

  • X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);

Edit:

- :

X509Certificate2Collection col = store.Certificates;

foreach (var currCert in col)
{
     var currThumbprint = currCert.Thumbprint;
     if (thumbprint.ToUpperInvariant() == currThumbprint)
     {
         x509Certificate2= currCert;
         break;
     }
}
+3

/ , . , ( "35ED.." ). , .

+2

All Articles