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 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 certs = new List();
certs.Add(cert.Thumbprint);
request.Headers.Add("Thumbprint", certs);
BearerToken token = null;
using (var httpClient = new HttpClient())
{
var tokenRequest =
new List>
{
new KeyValuePair("grant_type", "password"),
new KeyValuePair("username", objusers.username),
new KeyValuePair("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().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
[HttpPost]
public async System.Threading.Tasks.Task
{
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
certs.Add(cert.Thumbprint);
request.Headers.Add("Thumbprint", certs);
BearerToken token = null;
using (var httpClient = new HttpClient())
{
var tokenRequest =
new List
{
new KeyValuePair
new KeyValuePair
new KeyValuePair
};
HttpContent encodedRequest = new FormUrlEncodedContent(tokenRequest);
HttpResponseMessage response = httpClient.PostAsync(address, encodedRequest).Result; //It throws error here
if (response.IsSuccessStatusCode)
{
token = response.Content.ReadAsAsync
Session["ApiAccessToken"] = token.access_token;
}
else
{
Session["ApiAccessToken"] = "";
}
}
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");
}
}
Jeeva SubburajPosted Jun 13, 2019, 9:02 AM