1

我收到了一个 P12 证书,我需要使用该证书从 Azure 辅助角色调用 Web 服务。我上传了证书,我把它变成了一个 pem 文件并以编程方式添加它,但我总是在 GetRequest 上得到异常:

The request was aborted: Could not create SSL/TLS secure channel.

我知道 p12 还包含一个私钥,所以这可能是问题......

String strPem = "***valid PEM in BAS64***";
Uri uriGateway = new Uri("https://xxxx");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriGateway);
byte[] baPem = Convert.FromBase64String(strPem);
ServicePointManager.Expect100Continue = true;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(AcceptAllCertifications);
X509Certificate2 cert = new X509Certificate2(baPem);
request.Method = "GET";
request.ClientCertificates.Add(cert);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
4

1 回答 1

0

我找到了解决方案。

创建 x509Certificate 并使用 Import 方法导入 p12 证书文件。

var certificateFileName = Server.MapPath("~/Cert/cert.pfx");
X509Certificate2 cert = new X509Certificate2();
cert.Import(certificateFileName,"pw",X509KeyStorageFlags.PersistKeySet);

现在它起作用了......

于 2013-10-29T12:43:09.787 回答