X509Certificate2.ValidFrom Property

Gets the date and time when the certificate becomes valid.

Declaration
public DateTime ValidFrom { get; }
Property Value
A DateTime object that contains the date and time when the certificate becomes valid.
Implements
IX509Certificate.ValidFrom

Remarks

The ValidFrom property represents the date and time when the certificate is considered valid. This value is typically set by the Certificate Authority (CA) that issued the certificate.

To verify if a certificate is currently valid, you should compare the current system time with both the ValidFrom and ValidTo properties.

Example

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


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

public class CertificateInfo
{
    public static void Main(string[] args)
    {
        try
        {
            // Assuming you have a certificate stored in the personal store
            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);

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

                Console.WriteLine($"Certificate Subject: {certificate.Subject}");
                Console.WriteLine($"Valid From: {certificate.ValidFrom}");
                Console.WriteLine($"Valid To: {certificate.ValidTo}");

                // Check if the certificate is currently valid
                bool isCurrentlyValid = DateTime.UtcNow >= certificate.ValidFrom && DateTime.UtcNow <= certificate.ValidTo;
                Console.WriteLine($"Is Certificate Currently Valid: {isCurrentlyValid}");

                certificate.Reset(); // Dispose of the certificate
            }
            else
            {
                Console.WriteLine("No certificates found in the personal store.");
            }

            store.Close();
        }
        catch (CryptographicException e)
        {
            Console.WriteLine($"A Cryptographic error occurred: {e.Message}");
        }
        catch (Exception e)
        {
            Console.WriteLine($"An unexpected error occurred: {e.Message}");
        }
    }
}
        

Requirements

Namespace

System.Net.Security

Assembly

System.Security.Cryptography.X509Certificates.dll

Platform Compatibility

Windows, macOS, Linux

Note: The time returned by ValidFrom is based on the system clock of the computer where the certificate was issued. Ensure your system clock is synchronized for accurate validation.

See Also