Что можно сделать если в IIS SSL сертификат не работает, хотя локально работает?
Создал два проекта Web Api(back) и MVC(front). Web Api(back) опубликован в IIS, а MVC(front) запускаю локально. В проекте Web Api есть POST запрос в котором используется SSL сертификат. Между MVC(front) и Web Api(back) я общаюсь через GET/POST запросы. Сайт работает, проходит авторизацию и отображаются отчеты. Но если я вызываю с MVC(front) метод, который использует SSL сертификат в Web Api(back), то выходит ошибка: One or more errors occurred. (The SSL connection could not be established, see inner exception.).
А вот если я запускаю локально Web Api(back) на серваке и локально MVC(front) на серваке, и делаю запрос на тот же самый метод, но у меня проходит все без ошибки.
Вот код с Фронта
public string Identity(RequestClass<IdentificationModel> request)
{
ResponseClass<decimal> response = new ResponseClass<decimal>();
using (var httpClient = new HttpClient())
{
var json = JsonConvert.SerializeObject(request);
var data = new StringContent(json, Encoding.UTF8, "application/json");
var httpResponse =
httpClient.PostAsync(_apiUrl + $"Identification", data).Result;
string responseContent = httpResponse.Content.ReadAsStringAsync().Result;
return responseContent;
}
}
Вот код с Бэка
public decimal GetIdentification(IdentificationModel identification, int key)
{
try
{
var handler = new HttpClientHandler();
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"cert\\hgg.p12");
handler.ClientCertificates.Add(new X509Certificate2(path, _certPassword));
using (var httpClient = new HttpClient(handler))
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), _identityUrl + "iin=" + identification.IIN + "&vendor=" + _vendor))
{
string token = GetToken();
request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + token);
request.Headers.TryAddWithoutValidation("x-idempotency-key", "key:" + "hggKey-" + key);
request.Method = new HttpMethod("POST");
//request.Content = new StringContent(identification.Photo);
request.Content = new StreamContent(new MemoryStream(Convert.FromBase64String(identification.Photo)));
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("image/png");
var response = httpClient.SendAsync(request);
string data = response.Result.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<dynamic>(data).result;
}
}
}
catch (Exception e)
{
throw new Exception("GetIdentification" + e.Message);
}
}
public string GetToken()
{
var handler = new HttpClientHandler();
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "cert\\hgg.p12");
handler.ClientCertificates.Add(new X509Certificate2(path, _certPassword));
try
{
using (var httpClient = new HttpClient(handler))
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), _tokenUrl))
{
request.Headers.TryAddWithoutValidation("Authorization", "Basic " + _token);
request.Method = new HttpMethod("POST");
request.Content = new StringContent("grant_type=password&username=" + _username + "&password=" + _password + "&scope=identkey");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
var response = httpClient.SendAsync(request);
return JsonConvert.DeserializeObject<dynamic>(response.Result.Content.ReadAsStringAsync().Result).access_token;
}
}
}
catch(Exception e)
{
throw new Exception("GetToken" + e.Message + path + " " +_certPassword);
}
}
}
Ответы (1 шт):
ApplicationPoolIdentity не имеет разрешение для хранилищ сертификата, поэтому в IIS он не работал