X509Certificate2.Version Property

Gets the version number of the certificate.

Syntax

public string Version { get; }

Property Value

Type Description
string A string representing the version number of the certificate. This value is typically in the format "V1", "V2", etc.

Remarks

The Version property returns a string that represents the version number of the X.509 certificate. This property is read-only and is populated when the certificate is loaded or created.

The version number is part of the X.509 certificate standard and indicates the version of the certificate structure being used. Common values include "V1" (Version 1) and "V3" (Version 3). The actual string returned might vary based on the certificate's contents and how it was generated.

Examples

The following C# code example demonstrates how to retrieve and display the version of an X.509 certificate.


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

public class CertificateVersionExample
{
    public static void Main(string[] args)
    {
        try
        {
            // Load a certificate from the current user's personal store
            // You can replace "My" with "Root", "TrustedPublisher", etc. as needed.
            // And specify a thumbprint or name to select a specific certificate.
            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);

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

                Console.WriteLine($"Certificate Subject: {certificate.Subject}");
                Console.WriteLine($"Certificate Issuer: {certificate.Issuer}");
                Console.WriteLine($"Certificate Version: {certificate.Version}");
            }
            else
            {
                Console.WriteLine("No certificates found in the specified store.");
            }
            store.Close();
        }
        catch (CryptographicException ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An unexpected error occurred: {ex.Message}");
        }
    }
}
            

Requirements

Component Version
.NET Framework Supported in: 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, Client Profile
.NET Core Supported in: 1.0, 1.1, 2.0, 2.1, 2.2, 3.0, 3.1
.NET Supported in: 5.0, 6.0, 7.0, 8.0
Visual Studio Initially available in Visual Studio 2005.