MailBee.NET Objects 7.2

Certificate.Validate Method ()

Checks if the certificate is valid.

public CertificateValidationFlags Validate();

Return Value

A bitwise combination of CertificateValidationFlags indicating criteria the validation failed for, or None if the certificate is valid.

Remarks

You may get IsUntrustedRoot value returned by this method even if you know the certificate is trusted. This may happen if the certification authority (CA) which issued the given certificate is not present in the system CA store. This often happens with ASP.NET applications because ASP.NET user by default has only a few CA's in its system store. You can either add more CA's there or export your CA (one or more) into a file and use it as an extra store. See Validate overload or ASP.NET S/MIME demo projects shipped with the product for more details.

Exceptions

Exception Type Condition
MailBeeCertificateWin32Exception Win32 returned an error during during processing the certificate data.

Example

This sample loads the certificate store from disk and validates all the certificates contained in this store.

[C#]
// To use the code below, import MailBee namespace at the top of your code.
using System;
using MailBee.Security;

// The actual code (put it into a method of your class).
CertificateStore store = new CertificateStore(@"C:\Temp\certificate.p7b", CertStoreType.PublicFile, null);
CertificateCollection coll = store.GetAllCertificates();

foreach (Certificate cert in coll)
{
    Console.WriteLine(cert.Name);
    CertificateValidationFlags status = cert.Validate();
    if (status != CertificateValidationFlags.None)
    {
        Console.WriteLine("Untrusted");
        if ((status & CertificateValidationFlags.IsNotTimeValid) > 0)
        {
            Console.WriteLine("Certificate expired!");
        }
    }
    else
    {
        Console.WriteLine("Trusted");
    }
    Console.WriteLine("--------------------------------");
}
[Visual Basic]
' To use the code below, import MailBee namespace at the top of your code.
Imports MailBee.Security

' The actual code (put it into a method of your class).
Dim store As CertificateStore = New CertificateStore("C:\Temp\certificate.p7b", CertStoreType.PublicFile, Nothing)
Dim coll As CertificateCollection = store.GetAllCertificates()

For Each cert As Certificate In coll
    Console.WriteLine(cert.Name)
    Dim status As CertificateValidationFlags = cert.Validate()

    If status <> CertificateValidationFlags.None Then
        Console.WriteLine("Untrusted")
        If ((status And CertificateValidationFlags.IsNotTimeValid) > 0) Then
            Console.WriteLine("Certificate expired!")
        End If
    Else
        Console.WriteLine("Trusted")
    End If
    Console.WriteLine("--------------------------------")
Next

See Also

Certificate Class | MailBee.Security Namespace | Certificate.Validate Overload List | CertificateValidationFlags