Class System.Net.Security.CertificatePolicy

Declaration
public static class CertificatePolicy

Inheritance Hierarchy

Remarks

This class is obsolete. Use the System.Net.Security.CertificationPolicy class instead.

Deprecated.

The System.Net.Security.CertificationPolicy class has been deprecated. Applications should migrate to using the NetworkCredential class for authentication and the X509Certificate class for certificate management.

The CertificatePolicy class provides a static method that allows you to control how the .NET Framework handles server certificate validation for secure connections.

In earlier versions of the .NET Framework, when an application established a secure connection (e.g., using SSL/TLS), the Framework would automatically validate the server's certificate against a set of trusted root certificates. If the validation failed, the connection would typically be terminated.

The CertificatePolicy class provided a mechanism to override this default behavior. By implementing a custom certificate policy, developers could specify custom logic for accepting or rejecting server certificates. This was often used in development or testing environments where self-signed certificates were used, or when dealing with specific certificate authorities.

However, this approach had significant security implications. Allowing arbitrary certificate validation could expose applications to man-in-the-middle attacks. Therefore, this class has been deprecated in favor of more secure and standardized methods for certificate management.

Methods

Examples

Example of how to implement a custom certificate policy (for demonstration purposes only). In production environments, this approach is discouraged due to security risks.

// This code is for illustration and demonstration purposes only.
// Do not use in production environments without careful security review.

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

public static class Program
{
    public static void Main(string[] args)
    {
        // Set a custom certificate policy (demonstration)
        // In newer .NET versions, this static method is not the recommended approach.
        // You would typically use HttpClientHandler.ServerCertificateCustomValidationCallback.

        // Original approach (now obsolete):
        // ServicePointManager.CertificatePolicy = new MyCustomCertificatePolicy();

        // Example of how a custom policy might work (conceptual):
        // If you were to implement a custom policy, it would look something like this:

        // ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        // {
        //     // In a real scenario, you'd add logic here to inspect certificate and errors.
        //     // For example, to accept all certificates (highly insecure):
        //     return true;
        // };

        Console.WriteLine("Certificate policy demonstration.");
        // Simulate making a secure web request...
    }
}

// Obsolete custom policy class (conceptual)
// [Obsolete("This class is obsolete. Use ServicePointManager.ServerCertificateValidationCallback instead.")]
// public class MyCustomCertificatePolicy : ICertificatePolicy
// {
//     public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int error)
//     {
//         // Implement custom validation logic here.
//         // For example, to accept any certificate (insecure):
//         return true;
//     }
// }

Requirements

Namespace: System.Net.Security

Assembly: System (in System.dll)

See Also

Back to Top