CertificateSource Enumeration

Specifies the source of a certificate.

Note: This topic is part of the .NET Framework documentation.

Members

Member name Description
(()File) The certificate is from a file.
(()Store) The certificate is from a certificate store.
(()Unknown) The certificate source is unknown.

Remarks

The CertificateSource enumeration is used to indicate the location where a certificate was retrieved from. This information can be useful for debugging and for determining how to handle or validate certificates in different scenarios. For example, a certificate obtained from a trusted store might be treated differently than one loaded from a file.

Applies to

The members of this enumeration are commonly used with classes and methods within the System.Net.Security namespace, particularly those dealing with SSL/TLS connections and certificate validation.

Example

The following code example demonstrates how to check the source of a certificate after it has been retrieved:


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

public class CertificateSourceExample
{
    public static void Main(string[] args)
    {
        try
        {
            // Assume cert is obtained from somewhere, e.g., a file or a store
            // For demonstration, we'll create a placeholder. In a real scenario,
            // you would load a certificate here.
            X509Certificate2 cert = new X509Certificate2("path/to/your/certificate.cer");

            // In a real scenario, you might need to explicitly map the certificate
            // to its source if the X509Certificate2 object doesn't directly expose it.
            // For the purpose of this example, let's assume we know the source.

            CertificateSource source = CertificateSource.File; // Or fetched from logic

            Console.WriteLine($"Certificate source: {source}");

            switch (source)
            {
                case CertificateSource.File:
                    Console.WriteLine("The certificate was loaded from a file.");
                    break;
                case CertificateSource.Store:
                    Console.WriteLine("The certificate was retrieved from a certificate store.");
                    break;
                case CertificateSource.Unknown:
                    Console.WriteLine("The source of the certificate is unknown.");
                    break;
                default:
                    Console.WriteLine("An unexpected certificate source was encountered.");
                    break;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}
                    

Requirements

Assembly DLL
System.dll System.dll