Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not able to get X509Certificate2 certificate by code C#

I want to find certificate from my store but using following code i could not able to get certificate. It always return null.

code to get certificate by thumbprint

What's wrong with my code?

Update:

I have copied my certificate thumbprint by exploring store object and compare it with my thumbprint string and it return false! I think issue of interpreting string in VS2010 IDE or copy paste problem you can see that below in fig. because of this it should ignore the certificate from the list. Have anyone faced this type of issue before?

enter image description here

like image 593
Arun Rana Avatar asked Dec 31 '25 22:12

Arun Rana


1 Answers

Well the certificate collection is empty since there's no certificate with that thumbprint. Check:

  • that the certificate is present in your current user

  • that the certificate is stored in the personal folder

Try:

  • using mmc to verify the above things

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

Edit:

Does the following return anything useful:

X509Certificate2Collection col = store.Certificates;

foreach (var currCert in col)
{
     var currThumbprint = currCert.Thumbprint;
     if (thumbprint.ToUpperInvariant() == currThumbprint)
     {
         x509Certificate2= currCert;
         break;
     }
}
like image 187
t3hn00b Avatar answered Jan 06 '26 05:01

t3hn00b



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!