Search code examples
c#iisiis-express

Does calling the same HttpWebRequest from IIS lead to certificate error in IIS Express?


My IP address was white listed to be able to connect to a server on our work network so I can connect to it from a Webforms website I have been updating for a client. I have made an HttpWebRequest from that website that connects to the server to which my IP has been white listed and all is well. Now I want to connect the SAME code to an API that I am working on and I receive the following error:

System.Net.WebException: 'The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.'

I am running the Webforms site via IIS and the API via IIS Express. Is this difference in configuration the source of my problem?

I am calling the API from a local (e.g. C:\Myfile) HTML file with a linked javascript file, as such it is not in any type of project such as the Webforms project. I thought that might be causing the discrepancy too.

var nodeData = Encoding.ASCII.GetBytes("{\"path\": \"" + packageString.Trim() + "\"}");
var nodeRequest = (HttpWebRequest)WebRequest.Create("https://mytransfersite.org/ ");
var nodeAuthHeader = "Basic " + WebConfigurationManager.AppSettings["Auth"];

nodeRequest.Headers.Add(HttpRequestHeader.Authorization, nodeAuthHeader);
nodeRequest.ContentLength = nodeData.Length;
nodeRequest.Method = "POST";
nodeRequest.ContentType = "application/json";
nodeRequest.Accept = "application/json";
using (var stream = nodeRequest.GetRequestStream())
{
  stream.Write(nodeData, 0, nodeData.Length);
}

The error occurs on the nodeRequest.GetStream() call.


Solution

  • I forgot I added the following to the first bit of code to make it work:

    System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };