Search code examples
c#x509certificate

How to get the thumbprint of a X509Certificate certificate?


To validate in the client the certificate that it receives from the server, the validation method has a parameter of type X509Certificate that is the server certificate.

When I debug, I can see that it has a Thumbprint property, but when I try to access to them in the way myCertificate.Thmbprint I get a compiler error that it says that the property doesn't exist.

However, the type X509Certificate2 has this property.

So how could I get the thmbprint to know if it is the expected certificate?

Thanks.


Solution

  • It sounds like you're in a context where you have the cert as X509Certificate rather than the more powerful X509Certificate2.

    Your two easy options:

    1. Upcast. (X509Certificate2 myCertificate2 = (X509Certificate2)myCertificate;)
    2. Use the GetCertHash() or GetCertHashString() methods (depending on if you want bytes or the hex string). GetCertHashString() is the same as the Thumbprint property.