Udai Mathur

Udai Mathur

  • 1.8k
  • 40
  • 1.5k

Web API with HTTPs

Jun 7 2019 7:28 AM
Hello,
 
I have created one WEB API and one MVC web application. Both hosted on to IIS.
 
I am able to consume WEB API thorugh MVC application.
 
NOW I changed the binding of both the applcation from http to https with self signed certificate.
But when I call web api from MVC application it gives me below given error.
 
"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."
 
My CODE:
  1. // POST: Login/Create  
  2. [HttpPost]  
  3. public async System.Threading.Tasks.Task<ActionResult> Create(Users objusers)  
  4. {  
  5.     try  
  6.     {  
  7.         // TODO: Add insert logic here         
  8.   
  9.         BearerToken token = null;  
  10.   
  11.         using (var httpClient = new HttpClient())  
  12.         {   
  13.             var tokenRequest =  
  14.                 new List<KeyValuePair<stringstring>>  
  15.                     {  
  16.                         new KeyValuePair<stringstring>("grant_type""password"),  
  17.                         new KeyValuePair<stringstring>("username", objusers.username),  
  18.                         new KeyValuePair<stringstring>("password", objusers.password)  
  19.                     };  
  20.   
  21.             HttpContent encodedRequest = new FormUrlEncodedContent(tokenRequest);  
  22.   
  23.             HttpResponseMessage response = httpClient.PostAsync("https://localhost:1100/Token", encodedRequest).Result;  
  24.   
  25.             if (response.IsSuccessStatusCode)  
  26.             {  
  27.                 token = response.Content.ReadAsAsync<BearerToken>().Result;  
  28.   
  29.                 Session["ApiAccessToken"] = token.access_token;  
  30.             }  
  31.             else  
  32.             {  
  33.                 Session["ApiAccessToken"] = "";  
  34.             }  
  35.         }  
  36.   
  37.         List<BenefitElections> benefitelections = new List<BenefitElections>();  
  38.   
  39.         if (Session["ApiAccessToken"].ToString() != "")  
  40.         {  
  41.             using (var httpClient = new HttpClient())  
  42.             {  
  43.                 httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Session["ApiAccessToken"].ToString());  
  44.   
  45.                 var response = httpClient.GetAsync("https://localhost:1100/api/benefit/169548230").Result;  
  46.   
  47.                 if (response.IsSuccessStatusCode)  
  48.                 {  
  49.                     var data = await response.Content.ReadAsStringAsync();  
  50.                     //var myObj = data.Substring(1, data.Length - 2);  
  51.   
  52.                     benefitelections = JsonConvert.DeserializeObject<List<BenefitElections>>(data.ToString());  
  53.                 }  
  54.             }  
  55.   
  56.             return View("Index", benefitelections);  
  57.         }  
  58.         else  
  59.         {  
  60.             return View("Create");  
  61.         }  
  62.         //return RedirectToAction("Index", benefitelections);  
  63.           
  64.     }  
  65.     catch (Exception Ex)  
  66.     {  
  67.         Ex.ToString();  
  68.         return View("Create");  
  69.     }              
  70. }  
This code is working fine in case of http binding.

Answers (6)