I am facing issue with SSL certificates in IIS (w3wp.exe) processes. Recently we implemented SSL for our applications.
On developer machines we are using the IIS Express Development Certificate that comes as part of IIS installation. This certificates the 'localhost' hostname for SSL usage. In the IIS there are three different apps (with separate app pools) running. All the apps works fine when accessed via browser, everything is safe and sound.
We have some functionality where one of the apps (w3wp processes) requests data from another app/process. In order to have a single source of the data, we made it so that the process sends post request to the service that can provide the data.
This is done simply using LongTimeoutWebClient
like so:
using (LongTimeoutWebClient webClient = new LongTimeoutWebClient())
{
webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");
serverResponse = webClient.UploadString(serviceUri, "POST", serializer.Serialize(envelope));
}
As for the serviceUri, it is set correctly https://localhost/Service.svc
.
However I am getting following exception System.Net.WebException: 'The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.'
This happens only on localhost and my guess is that it is because the certificate is self-signed - issued by localhost for localhost.
I know I can work around this by having custom certificate validation callback
ServicePointManager.ServerCertificateValidationCallback =
(sender, certificate, chain, errors) =>
{
if (errors == SslPolicyErrors.None)
return true;
if (sender is HttpWebRequest request)
return request.RequestUri.Host == "localhost";
return false;
};
My question however is: is there a easier/more elegant solution to this problem? Basically what this workaround says is that on localhost I am trusting all requests as long as they target localhost. Which is fundamentally not wrong, it's just that it doesn't feel right.