Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure api The request was aborted: Could not create SSL/TLS secure channel. http client While calling some web api

Below code works fine on localhost but Getting the above error after deployed my api on microsoft azure .

My code is as follows:

public class UsersController : Controller   
{
    private readonly HttpClient _client;

    public UsersController()
    {
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
        _client = new HttpClient { BaseAddress = new Uri("https://pincode.saratchandra.in/api") };
        _client.DefaultRequestHeaders.Accept.Clear();
        _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    }

public JsonResult GetAddress(int pincode)
{
    var response     =_client.GetAsync("/pincode/125112").Result;

    //Here it throws an exception: got no response
    if (response.IsSuccessStatusCode)
    {
    }

    return Json(ApiResult.Success(), JsonRequestBehavior.AllowGet);
}

Didn't get any response for above call just get the error

The request was aborted: Could not create SSL/TLS secure channel

like image 996
jitender Avatar asked May 19 '16 13:05

jitender


People also ask

What causes the request was aborted could not create SSL TLS secure channel?

The error “The request was aborted: Could not create SSL/TLS secure channel.” can happen during any download HTTP request. This error generally will correspond to firewalls, proxies or DNS filtering blocking the connection or an SSL/TLS cipher misconfiguration.

Can't create SSL TLS secure channel IIS?

However, the "Could not create SSL/TLS secure channel" error usually means that there's something wrong with the server certificate, e.g. the certificate is for a different hostname, or otherwise invalid, or not trusted etc.

Could not establish a secure SSL TLS connection to the requested server host?

A common reason you may receive the error Could not establish trust relationship for the SSL/TLS secure channel is because the SSL certificate isn't trusted. If the SSL certificate is not trusted, you will need to install the SSL certificate's root certificate.


1 Answers

We have recently had a number of similar issues with API's.

In our case these were resolved by changing the Security Protocol Type to TLS 1.2, like so:

  ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

This appears to be a result of the deprecation of SSL, and the recommendation of the adoption of TLS 1.2 by the PCI as of the middle of 2016. See here for more

EDIT:

This works for me:

 class Program
    {
        private static HttpClient _client;
        static void Main(string[] args)
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
            _client = new HttpClient ();
            _client.DefaultRequestHeaders.Accept.Clear();
            _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var response = _client.GetAsync("https://pincode.saratchandra.in/api/pincode/125112").Result;
        }
    }
like image 62
Matt Evans Avatar answered Sep 28 '22 03:09

Matt Evans