TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Udai Mathur
NA
49
11.1k
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:
// POST: Login/Create
[HttpPost]
public
async System.Threading.Tasks.Task<ActionResult> Create(Users objusers)
{
try
{
// TODO: Add insert logic here
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();
//var myObj = data.Substring(1, data.Length - 2);
benefitelections = JsonConvert.DeserializeObject<List<BenefitElections>>(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.
Reply
Answers (
6
)
what are the file needed to hosting
how to click to add parent class in javascript??