Solve “System.Threading.Tasks.TaskCanceledException : The operation was canceled. ” on Ubuntu 18.04 in .Net Core 2.2

I am running some .Net Core 2.2 (at the moment of writing version 2.2.301) integration test in Azure DevOps (ADO) running on my private Linux Ubuntu 18.04 agents.
The tests are quiet simple, just call an internally exposed Rest API through Azure API Management.
Because the APIM is running internally the endpoint are using self-signed SSL certificates generated with an internal Root Certificate.

During the runs in ADO I was experiencing the following error:

Error Message:
   System.Threading.Tasks.TaskCanceledException : The operation was canceled.
  Stack Trace:
     at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)

But it was strange, I was expecting self-signed certificate errors, so my code is looking like this:

    class Program
    {
        static void Main(string[] args)
        {
            var handler = new System.Net.Http.HttpClientHandler();
            handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
            handler.SslProtocols =
                System.Security.Authentication.SslProtocols.Tls12 |
                System.Security.Authentication.SslProtocols.Tls11 |
                System.Security.Authentication.SslProtocols.Tls;

            using (var httpClient = new System.Net.Http.HttpClient(handler))
            {
                try
                {
                    var stringTask = httpClient.GetAsync("https://<<THE_REST_CALL>>");
                    Console.WriteLine(stringTask.Result);
                }
                catch (HttpRequestException ex)
                {
                    Console.WriteLine($"Error: {ex}");
                }
            }
        }
    }

As you can see I am using the DangerousAcceptAnyServerCertificateValidator delegate which should return true for any invalid certificate.
And it was indeed working on my Windows machine (and the root certificate is not installed on my machine).

I will save you all my long research, but all the solutions I found were not working. Until….:

Save the self-signed root certificate in the cert store:

I first exported the certificate from a (Windows) machine were it was installed:

And saved as my-root.crt certificate.

Then I copied the root certificate in my Ubuntu 18.04 Private Agent with:

sudo cp my-root.crt /usr/local/share/ca-certificates/my-root.crt
sudo update-ca-certificates

And after running my integrations tests everything was working fine:

Test Run Successful.
Total tests: 3
     Passed: 3
 Total time: 11.5062 Seconds

I have created a GitHub issue regarding this problem, maybe I will get some clarification why my code is not working on my Ubuntu 18.04 distro.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

%d bloggers like this: