Ignore HTTPS issues with HTTPClient in .NET core

For a proof-of-concept I had to communicate with a restful api build by another team. The api is using a self-signed certificate to make it work over https.
This would generate some https/ssl errors that would cause my HttpClient not to work.

So the first step to resolve this, was to add the following piece of code..

using (var handler = new HttpClientHandler())  
{
  handler.ServerCertificateCustomValidationCallback
  = 
  (message, cert, chain, errors) => true;

  using (var client = new HttpClient(handler))
  {
     //...
  }
}

If you're running your .net core application on Mac OSX or Linux you might run into a PlatformNotSupportedException telling you something like this:

System.PlatformNotSupportedException: The libcurl library in use (7.51.0) and its SSL backend ("SecureTransport") do not support custom handling of certificates. A libcurl built with OpenSSL is required.  

It took me a while to figure out how to resolve this, but here is the solution on Mac OSX:

brew install curl --with-openssl && brew link curl —force  

After that set this environment variable, forcing the runtime to use the newly installed version of curl (instead of the default OS version)

export DYLD_LIBRARY_PATH=/usr/local/lib/  

It will work from the command line now!

If you are running&debugging with Visual Studio Code (like I am), you need add this environment setting to .vscode/launch.json:

"env": {
    "ASPNETCORE_ENVIRONMENT": "Development",
    "DYLD_LIBRARY_PATH": "/usr/local/lib/"
}

Now, when you can run your application from Visual Studio code as well!

Ronald Harmsen

I'm a software developer. When I'm not developing software I'm training & coaching other developers, speaking on a conference or fiddling with some technical stuff.

Arnhem, The Netherlands

Subscribe to Ronald to the cloud

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!