MSDN Library

X509Certificate2.SerialNumber Property

Declaration

public string SerialNumber { get; }

Remarks

The SerialNumber property gets the serial number of the X.509 certificate.

The serial number is a string representation of the certificate's serial number, typically in hexadecimal format.

  • The serial number is a required field for X.509 certificates.
  • It is used to uniquely identify a certificate issued by a specific Certificate Authority (CA).
  • This property can be useful when you need to compare certificates or perform lookups based on the serial number.

Return Value

A string that contains the serial number of the certificate.

Example

The following example displays the serial number of a certificate.

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

public class Example
{
    public static void Main(string[] args)
    {
        try
        {
            // Load a certificate from the CurrentUser's My store
            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);

            X509Certificate2Collection certCollection = store.Certificates;
            if (certCollection.Count > 0)
            {
                X509Certificate2 cert = certCollection[0]; // Get the first certificate

                Console.WriteLine($"Certificate Subject: {cert.Subject}");
                Console.WriteLine($"Serial Number: {cert.SerialNumber}");
            }
            else
            {
                Console.WriteLine("No certificates found in the CurrentUser's My store.");
            }
            store.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}