我收到了一个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();

最佳答案

我找到了解决方案。

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

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


现在就可以了...

关于c# - 在Azure上使用pkcs#12调用服务,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19633628/

10-10 10:08