Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restsharp raise SSL error when run on server

I am using Restsharp request to an API and codes are blow:

var client = new RestClient(baseURL);
var request = new RestRequest(url);
request.AddHeader("Authorization", authHeader);

var response = client.Get<T>(request);
return response;

Same code did not raise any error on my test enviroment, but when I run the code on server, I get following error:

error message    : The SSL connection could not be established, see inner exception. The remote certificate is invalid according to the validation procedure.
exception        : System.Net.WebException: The SSL connection could not be established, see inner exception. The remote certificate is invalid according to the validation procedure. ---> System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.

I don't know what is wrong with that code and I don't understand why same codes run differently?

Any help for fix that?

like image 651
serhattsnmz Avatar asked Oct 19 '25 14:10

serhattsnmz


1 Answers

Seems like the service pointed by baseURL has invalid SSL certificate. You can bypass SSL validation check by adding the following line of code before making the call.

ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

This globally disables SSL validation. It will totally work fine even if the production service has valid SSL, but it will certainly introduce some vulnerability in your code. You can wrap it inside

#if DEBUG
#endif
like image 54
rabink Avatar answered Oct 21 '25 04:10

rabink



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!