在azure上用pkcs#12调用服务

时间:2013-10-28 11:29:59

标签: c# azure ssl certificate

我收到了一份P12证书,我需要使用该证书从Azure辅助角色调用Web服务。我上传了证书,我将其转换为pem文件并以programmaticaly方式添加,但我总是在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();

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

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

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

现在它有效......