Reputation: 5230
I'm having real issues attempting to contact a secure rest URL. Basically I have little knowledge of certificates, and am wondering whether what I am doing with the certificate file(s) is correct.
When running req.GetResponse() an exception occurs:
"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."
Inner Ex: "The remote certificate is invalid according to the validation procedure."
I'm currently unsure as to whether things need a little tinkering or I'm simply way off. code is basically...
if (!File.Exists(certificateLocation))
{
throw new Exception(string.Format("The specified certificate file does not exist: {0}", certificateLocation));
}
//Cert Challenge URL
Uri requestURI = new Uri(url);
//Create the Request Object
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(requestURI);
//Set the Request Object parameters
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
req.AllowAutoRedirect = false;
//Create certificate from our file
X509Certificate cert = X509Certificate.CreateFromCertFile(certificateLocation);
req.ClientCertificates.Add(cert);
WebResponse response = req.GetResponse(); // *** Errors here
...
Edit: Currently I'm only attempting the "read" url - seemed logical.
After contacting the issuer for the password accompanying the .p12 certificate, and importing that into the "Trusted Root Certification Authorities section" of certification manager, the error has now changed to the following...
System.Net.WebException
"The underlying connection was closed: An unexpected error occurred on a receive."
Inner Ex:
"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."
Upvotes: 1
Views: 789
Reputation: 5302
-- re-posting comment as answer --
The password is required for this to work.
It is created at the same time as the .p12
file.
Upvotes: 1