5

I am trying to start a REST request with RestSharp on a server that obviously has no valid ssl certificate, as I get the error

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

I found this question but the provided solution, as simple as it may look doesn't work, I still get the error. Here is my code, any ideas what might be wrong?

private void Test_REST()
{
  var client = new RestClient("https://online.gema.de");

  // both versions not working
  ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
  client.RemoteCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

  // execute the request
  IRestResponse response = client.Execute(GEMA_Authorize());
  var content = response.Content; // raw content as string
}

private RestRequest GEMA_Authorize()
{
  RestRequest request = new RestRequest("api/v1/authorize", Method.GET);
  request.AddParameter("response_type", "token"); 
  request.AddParameter("client_id", "test_client");
  request.AddHeader("Accept", "application/json");
  request.AddHeader("Content-Type", "application/x-www-form-urlencoded");

  return request;
}

When I write the Callback like this:

  .ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) =>
  {
    return true;
  };

and set a breakpoint at return true; the execution doesn't stop there, so it seems that the callback is never called back. Any ideas what might be the issue? I'm rather new to REST, so I might miss something crucial.

Thanks in advance,
Frank

Aaginor
  • 4,516
  • 11
  • 51
  • 75
  • 1
    The client callback is not an event handler but a delegate. Why are you assigning them with `+=` when you need to use `=`? RestSharp has no SSL handling. It uses `HttpWebRequest` and the callback assignment should work. `client.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;` should be fine. – Alexey Zimarev Mar 07 '18 at 19:03
  • Hey Alexey, thank you very much for the response. I used the += because this is the way it was used by the linked SO-question. I tried it with your construction, still no success, the error remains. Setting a breakpoint reveals that the callback is still not called. – Aaginor Mar 08 '18 at 12:45
  • 1
    FWIW, I had this issue, found this Q from the callback name, and used the line: client.RemoteCertificateValidationCallback = (sender, certificate, chain, errors) => true; and it "just worked" for me. So I can't help the specific question, but perhaps might help others who find it... – mj2008 Aug 23 '18 at 13:40

1 Answers1

3

The usage of

client.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;

worked for me.

jfmg
  • 2,626
  • 1
  • 24
  • 32