Could not able to get X509Certificate2 certificate by code C#

喜欢而已 提交于 2020-01-23 11:27:35

问题


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

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?


回答1:


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;
     }
}



回答2:


There might be invisible/zero length chars in your thumbprint especially at the beginning or at the end. Measure the length of thumbprint you supplied ("35ED.." ). It should be greater than what appears.



来源:https://stackoverflow.com/questions/10652073/could-not-able-to-get-x509certificate2-certificate-by-code-c-sharp

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