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
10.9k
Not Able to consume web api (hosted in https) from mvc
Jun 12 2019 6:15 AM
I have created self sigend certificate in IIS.
Changed Edit binding of my web api and mvc appication with https and selected the newly created certificate.
In mmc first exprorted the certificate and then inported it with in personal folder.
I have modified my code and still I am getting the same 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
string actionURI = "https://localhost:1100/";
Uri address = new Uri(actionURI);
HttpRequestMessage request = new HttpRequestMessage
(HttpMethod.Get, address); // HttpMethod.Post is also works
var cert = new X509Certificate2(@"D:\Cer\Demo.cer");
List<string> certs = new List<string>();
certs.Add(cert.Thumbprint);
request.Headers.Add("Thumbprint", certs);
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(address, encodedRequest).Result; //It throws error here
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");
}
}
Reply
Answers (
1
)
alertify is not waiting for confirmation in a check box
Cheap Linux Hosting