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:
- // POST: Login/Create
- [HttpPost]
- public async System.Threading.Tasks.Task
Create(Users objusers) - {
- try
- {
- // TODO: Add insert logic here
- BearerToken token = null;
- using (var httpClient = new HttpClient())
- {
- var tokenRequest =
- new List
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
().Result; - Session["ApiAccessToken"] = token.access_token;
- }
- else
- {
- Session["ApiAccessToken"] = "";
- }
- }
- List
benefitelections = new List (); - 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();
- //var myObj = data.Substring(1, data.Length - 2);
- benefitelections = JsonConvert.DeserializeObject
- >(data.ToString());
- }
- }
- return View("Index", benefitelections);
- }
- else
- {
- return View("Create");
- }
- //return RedirectToAction("Index", benefitelections);
- }
- catch (Exception Ex)
- {
- Ex.ToString();
- return View("Create");
- }
- }
This code is working fine in case of http binding.
Udai MathurPosted Jun 11, 2019, 2:14 AM
>(data.ToString());
Vimlesh TiwariPosted Jun 9, 2019, 11:25 AM
Jignesh KumarPosted Jun 8, 2019, 6:56 AM
Madan ShekarPosted Jun 7, 2019, 7:56 AM
Udai MathurPosted Jun 7, 2019, 7:52 AM
Madan ShekarPosted Jun 7, 2019, 7:31 AM
https://www.c-sharpcorner.com/UploadFile/anavijai/could-not-establish-trust-relationship-for-the-ssltls-secure-channel/