使用用户证书签名的 HTTPS 连接 - 不同计算机上的意外行为
本文关键字:计算机 意外 连接 用户 证书 书签 HTTPS | 更新日期: 2023-09-27 18:35:25
有一个服务。我必须通过HTTPS协议连接到它,通过POST方法上传数据,然后下载结果。连接使用用户私有证书(*.p12 格式)进行身份验证。
我编写了以下函数来实现它:
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;
}
在我的计算机上一切正常 - 没有异常,错误,数据已成功上传,服务有适当的响应。
但是在所有其他PC上,标有<====== 3的行都有例外,并带有消息:
请求已中止:无法创建 SSL/TLS 安全通道。
我使用相同的证书文件、相同的地址和相同的数据。结果是错误,我无法弄清楚,出了什么问题。
有人可以帮忙吗?
好的,我找到了解决方案。
证书必须在操作系统中注册。否则,它将不起作用。
无论如何,感谢您的关注。