X509Certificate2.NotBefore Property
Gets the date and time at which the certificate becomes valid.
Syntax
public DateTime NotBefore { get; }
Property Value
A DateTime object that contains the date and time at which the certificate becomes valid.
Remarks
The NotBefore property represents the start of the validity period for the X.509 certificate. Certificates issued by a Certificate Authority (CA) have a validity period defined by a start date and an end date. This property indicates the earliest date and time when the certificate is considered valid by relying parties.
If the current system time is before the time specified by NotBefore, the certificate is considered not yet valid, and cryptographic operations that depend on its validity may fail or be rejected.
The value returned by this property is typically in Coordinated Universal Time (UTC), but applications should be mindful of potential time zone differences when comparing certificate validity periods with local system times.
Examples
The following C# code example demonstrates how to retrieve and display the NotBefore property of an X509Certificate2 object.
using System;
using System.Security.Cryptography.X509Certificates;
public class CertificateInfo
{
public static void Main(string[] args)
{
// Load a certificate (replace with your certificate loading logic)
try
{
string certificatePath = @"C:\path\to\your\certificate.cer";
X509Certificate2 certificate = new X509Certificate2(certificatePath);
DateTime validFrom = certificate.NotBefore;
Console.WriteLine($"Certificate is valid from: {validFrom.ToUniversalTime()}");
// Example of checking validity
if (DateTime.UtcNow < validFrom)
{
Console.WriteLine("Certificate is not yet valid.");
}
else
{
Console.WriteLine("Certificate is currently valid or has expired.");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
Requirements
| Product | Version |
|---|---|
| .NET Framework | Supported in all versions. |
| .NET Core | Supported in all versions. |
| .NET Standard | Supported in all versions. |