Gets the date and time when the certificate becomes valid.
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.
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}");
}
}
}
System.Net.Security
System.Security.Cryptography.X509Certificates.dll
Windows, macOS, Linux
ValidFrom is based on the system clock of the computer where the certificate was issued. Ensure your system clock is synchronized for accurate validation.