X509Certificate2.ValidFrom Property

Gets the date and time at which the X.509 certificate becomes valid.

Declaration

public DateTime ValidFrom { get; }

Property Value

Type: System.DateTime

The date and time at which the X.509 certificate becomes valid.

Remarks

The ValidFrom property represents the not-before date of the certificate. This is the earliest point in time when the certificate is considered valid. Any attempt to use the certificate before this date will result in an error.

This property is crucial for validating the authenticity and trustworthiness of a certificate. When checking a certificate, you should always compare the current date and time against both the ValidFrom and ValidTo properties to ensure the certificate is currently in effect.

Tip: For robust certificate validation, use the methods provided by the X509Chain class, which automatically handles checks for validity dates, revocation, and trust chains.

Examples

The following code example demonstrates how to retrieve and display the ValidFrom property of an X509Certificate2 object.


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

public class Example
{
    public static void Main(string[] args)
    {
        // Load a certificate from the Current User's My store
        // Replace "YourCertificateName" with the actual subject name or thumbprint of your certificate
        X509Certificate2 certificate = null;
        X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        try
        {
            store.Open(OpenFlags.ReadOnly);
            X509Certificate2Collection certCollection = store.Certificates;
            // You might need to select a specific certificate from the collection
            // For demonstration, let's pick the first one if available
            if (certCollection.Count > 0)
            {
                certificate = certCollection[0];
            }
        }
        finally
        {
            store.Close();
        }

        if (certificate != null)
        {
            Console.WriteLine($"Certificate Subject: {certificate.Subject}");
            Console.WriteLine($"Certificate Issuer: {certificate.Issuer}");
            Console.WriteLine($"Valid From: {certificate.ValidFrom:yyyy-MM-dd HH:mm:ss}");
            Console.WriteLine($"Valid To: {certificate.ValidTo:yyyy-MM-dd HH:mm:ss}");

            // Check if the certificate is currently valid
            if (DateTime.UtcNow >= certificate.ValidFrom && DateTime.UtcNow <= certificate.ValidTo)
            {
                Console.WriteLine("The certificate is currently valid.");
            }
            else
            {
                Console.WriteLine("The certificate is NOT currently valid.");
            }
        }
        else
        {
            Console.WriteLine("No certificate found in the CurrentUser\\My store.");
        }
    }
}
        

Requirements

Namespace: System.Net.Security

Assembly: System (in System.dll)

.NET Framework versions: Supported in the following versions: 4.8, 4.7.2, 4.7.1, 4.7, 4.6.2, 4.6.1, 4.6, 4.5.2, 4.5.1, 4.5, 4.0, 3.5, 3.0, 2.0

.NET versions: Supported in the following versions: 7, 6, 5, Core 3.1, Core 2.1, Core 2.0, Standard 2.1, Standard 2.0

See Also

X509Certificate2 Class

System.Net.Security Namespace

X509Certificate2.ValidTo Property

X509Certificate2.Extensions Property