How to ignore "System.Net.Http.CurlException: a peer certificate cannot be authenticated with these CA certificates" on platforms other than Windows?

I deployed a Kubernetes cluster in Microsoft Azure and would like to call some of the REST APIs from .Net Core C # using https. Certificates used when deploying a cluster to a non-trusted CA. When I run this program on a Mac, I get the following error: "System.Net.Http.CurlException: Peer certificate cannot be authenticated with these CA certificates"

On Windows, I can set my own ServerCertificateValidationCallback to ignore the error:

    WinHttpHandler winHttpHandler = new WinHttpHandler();
    winHttpHandler.ServerCertificateValidationCallback = ValidateServerCertificate;

public static bool ValidateServerCertificate(
    HttpRequestMessage request,
    X509Certificate certificate,
    X509Chain chain,
    SslPolicyErrors sslPolicyErrors)
{
    return true;
}

But this is not supported in .Net Core on platforms other than Windows.

How to ignore the error on other platforms?

+4
1

.NET Core. 1.1, 2.0, 2.1.

, HttpHandler HttpClient. HttpHandler ServerCertificateCustomValidationCallback, .

:

private HttpClient SampleBuildHttpClient()
{
    return new HttpClient(
        new HttpClientHandler
        {
            ServerCertificateCustomValidationCallback = MyCallback,
        }); 
}

private bool MyCallback(HttpRequestMessage reqMsg, X509Certificate2 cert, X509Chain certChain, SslPolicyErrors policyErrors)
{
    //custom validation
    return true;
}
0

Source: https://habr.com/ru/post/1674209/


All Articles