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:
-
- [HttpPost]
- public async System.Threading.Tasks.Task<ActionResult> Create(Users objusers)
- {
- try
- {
-
-
- BearerToken token = null;
-
- using (var httpClient = new HttpClient())
- {
- var tokenRequest =
- new List<KeyValuePair<string, string>>
- {
- new KeyValuePair<string, string>("grant_type", "password"),
- new KeyValuePair<string, string>("username", objusers.username),
- new KeyValuePair<string, string>("password", objusers.password)
- };
-
- HttpContent encodedRequest = new FormUrlEncodedContent(tokenRequest);
-
- HttpResponseMessage response = httpClient.PostAsync("https://localhost:1100/Token", encodedRequest).Result;
-
- if (response.IsSuccessStatusCode)
- {
- token = response.Content.ReadAsAsync<BearerToken>().Result;
-
- Session["ApiAccessToken"] = token.access_token;
- }
- else
- {
- Session["ApiAccessToken"] = "";
- }
- }
-
- List<BenefitElections> benefitelections = new List<BenefitElections>();
-
- if (Session["ApiAccessToken"].ToString() != "")
- {
- using (var httpClient = new HttpClient())
- {
- httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Session["ApiAccessToken"].ToString());
-
- var response = httpClient.GetAsync("https://localhost:1100/api/benefit/169548230").Result;
-
- if (response.IsSuccessStatusCode)
- {
- var data = await response.Content.ReadAsStringAsync();
-
-
- benefitelections = JsonConvert.DeserializeObject<List<BenefitElections>>(data.ToString());
- }
- }
-
- return View("Index", benefitelections);
- }
- else
- {
- return View("Create");
- }
-
-
- }
- catch (Exception Ex)
- {
- Ex.ToString();
- return View("Create");
- }
- }
This code is working fine in case of http binding.