There is a service. I have to connect to it via HTTPS protocol, upload a data via POST method, and download result. The connection is authenticated with users private certificate, in *.p12 format.
I have written following function to achieve it:
static public string PostLPDA(string fileName, string URL, string certificateFile)
{
X509Certificate cert = new X509Certificate(certificateFile); // importing certificate from file <===== 1
StreamReader sr = new StreamReader(fileName);
string postString = string.Format("Query={0}", sr.ReadToEnd()); // preparing data to send
const string contentType = "application/x-www-form-urlencoded";
System.Net.ServicePointManager.Expect100Continue = false;
CookieContainer cookies = new CookieContainer();
HttpWebRequest webRequest = WebRequest.Create(URL) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = contentType;
webRequest.CookieContainer = cookies;
webRequest.ContentLength = postString.Length;
webRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
webRequest.ClientCertificates.Add(cert); //adding certificate
System.Net.ServicePointManager.ServerCertificateValidationCallback =
((sender, certificate, chain, sslPolicyErrors) => true); // ignore untrusted site error <===== 2
StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream()); // <===== 3
requestWriter.Write(postString);
requestWriter.Close();
StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();
webRequest.GetResponse().Close();
return responseData;
}
Everything works OK on my computer - there are no exceptions, errors, data is successfully uploading, there is proper response from service.
But on every other PC, there is exception on line marked with <====== 3, with message:
The request was aborted: Could not create SSL/TLS secure channel.
I'm using the same certificate file, same address, and same data. Result is error, and I cannot figure, what's wrong.
Somebody can help?
OK, i find the solution.
Certificate have to be registered in OS. Otherwise, it will not work.
Anyway, thanks for attention.