Verify a Digital Certificate in C#


void VerifyDigitalCertificate()
{
    X509Certificate2 cert = new X509Certificate2("certificate.cer");

    //CertificateStatus.Expired - the certificate is expired
    //CertificateStatus.Revoked - the certificate is revoked 
    //CertificateStatus.Unknown - the CRL or the OCSP service is unavailable
    //CertificateStatus.Valid - the certificate is OK

    Console.WriteLine("Verify against the local time: " +
    DigitalCertificate.VerifyDigitalCertificate(cert, VerificationType.LocalTime));
    
    Console.WriteLine("Verify against the CRL: " +
    DigitalCertificate.VerifyDigitalCertificate(cert, VerificationType.CRL));

    Console.WriteLine("Verify against the OCSP: " +
    DigitalCertificate.VerifyDigitalCertificate(cert, VerificationType.OCSP));

    Console.WriteLine("Verify against the LDAP: " +
    DigitalCertificate.VerifyDigitalCertificate(cert, VerificationType.LDAP));

    Console.WriteLine("Certificate subject:" + cert.Subject);
    Console.WriteLine("Issued by:" + cert.GetNameInfo(X509NameType.SimpleName, true));
    Console.WriteLine("Expiration date: " + cert.NotAfter);
    Console.WriteLine("Serial number: " + cert.SerialNumber);
    Console.WriteLine("Signature algorithm: " + cert.SignatureAlgorithm.FriendlyName);
}

See also: