1

C# SDK - ASP.NET Core 2.1 - The SSL connection could not be established

I tried

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

It is running in .NET Framework but not in ASP.NET Core.

I also tried

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

But I didn't get success in ASP.NET Core

I want to generate self SSL in asp.net core

Simply Ged
  • 8,250
  • 11
  • 32
  • 40
lalit sharma
  • 21
  • 1
  • 1
  • 2

2 Answers2

3

In .NET core, you should do this at the HttpClient level rather than use the ServicePointManager object, e.g.

https://stackoverflow.com/a/44540071/1538039

I answered a similar question to this here - https://stackoverflow.com/a/55358543/1538039

While you can bypass the validation in this way, it's not the correct approach if you control the server. You're basically saying 'I've configured the server in a way that means my client can't establish trust, because it doesn't implicitly trust the certificate chain. To get around this, I'm going to accept all errors by adding some code'

A better solution is to configure the server with a certificate the client can trust, and then things will just work!

At development time working locally, if you want to use a self-signed certificate then you can install it as a root CA and things should work as expected.

Once you've got to a stage where you don't control the client, this obviously isn't something you can ask people to do. The only thing to do then is to correctly configure the server with a certificate the client can trust, using something like letsencrypt to generate the cert.

Dylan Morley
  • 1,656
  • 12
  • 21
  • What ever certificate we configure in IIS need to be installed on the server (for the trust part). that worked for me. – Say Sep 20 '20 at 15:35
0

Below solution works for RestSharp library

var restClient = new RestClient(baseUrl);
restClient.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
Somnath Kadam
  • 6,051
  • 6
  • 21
  • 37