X509Certificate2.FindByThumbprint Method

Summary

Retrieves the first certificate that matches the specified thumbprint.

Syntax
public static X509Certificate2 FindByThumbprint(string thumbprint)
public static X509Certificate2 FindByThumbprint(string thumbprint, bool validOnly)
Parameters
Name Type Description
thumbprint string The thumbprint of the certificate to find. The thumbprint is the SHA-1 hash of the certificate.
This string can be in the following formats:
  • 16-byte hexadecimal string (e.g., "0000000000000000000000000000000000000000")
  • 16-byte hexadecimal string with hyphens (e.g., "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00")
validOnly bool true to search only for valid certificates (certificates that are not expired and have not been revoked); otherwise, false.
If this parameter is true, only certificates that are valid at the time of the search are returned.
Returns

An X509Certificate2 object that represents the certificate that matches the specified thumbprint, or null if no matching certificate is found.

Exceptions
Type Condition
ArgumentException The thumbprint parameter is not a valid thumbprint format.
CryptographicException An error occurred during cryptographic processing.
Remarks

The thumbprint is the SHA-1 hash of the certificate. It is a unique identifier for the certificate.

This method searches the certificate store for a certificate that matches the specified thumbprint.

If validOnly is set to true, the method will only return certificates that are currently valid.

Example

The following code example demonstrates how to use the FindByThumbprint method to retrieve an X509Certificate2 object.


using System;
using System.Security.Cryptography.X509Certificates;

public class Example
{
    public static void Main()
    {
        // Replace with a valid thumbprint from your system
        string thumbprintToFind = "0000000000000000000000000000000000000000";

        // Find the certificate using its thumbprint, only valid ones
        X509Certificate2 certificate = X509Certificate2.FindByThumbprint(thumbprintToFind, true);

        if (certificate != null)
        {
            // Certificate found, you can now use it
            Console.WriteLine("Certificate found:");
            Console.WriteLine("Subject: {0}", certificate.Subject);
            Console.WriteLine("Issuer: {0}", certificate.Issuer);
            Console.WriteLine("Thumbprint: {0}", certificate.Thumbprint);
        }
        else
        {
            Console.WriteLine("Certificate not found or is not valid.");
        }
    }
}
            
See Also