We use HttpWebRequest
to make web service calls to a remote server. The call requires a certificate to be loaded from the local machine.
The problem we are running into is if the Application Pool identity is set to NetworkService
then the call fails with a generic TLS/SSL error on the HttpWebResponse.GetResponse()
call. If we change the identity of the app pool user to LocalSystem
then it functions normally.
What I'm trying to figure out is why. What does LocalSystem
have access to that NetworkService
doesn't which is impacting a post call?
The code is pretty simple:
string URL = "https://mywebserver.net";
X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var certResults = store.Certificates.Find(X509FindType.FindBySubjectName, "nameofthecertificate", false);
/* I have confirmed that the certificate is found regardless of
* how the application pool is configured.
*/
X509Certificate cert = certResults[0];
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
req.AllowAutoRedirect = true;
req.ClientCertificates.Add(cert);
req.Method = "POST";
req.ContentType = "application/soap+xml;charset=UTF-8";
StringBuilder postData = new StringBuilder();
postData.Append("some soap info");
byte[] postBytes = Encoding.UTF8.GetBytes(postData.ToString());
using (Stream postStream = req.GetRequestStream())
{
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Flush();
postStream.Close();
}
// dies here when running under NetworkService
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
String data = String.Empty;
using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
{
data = reader.ReadToEnd();
reader.Close();
}