2013-10-28 26 views
1

我收到了我需要用來從Azure工作者角色調用Web服務的P12證書。我上傳的證書,我把它變成一個PEM文件和programmaticaly加入它,但我總是得到的GetRequest異常:在azure上使用pkcs#12撥打服務

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(); 
+0

你在哪裏上傳證書和以哪種方式?它應該上傳到管理證書部分,並且應該是cer文件格式。 –

+0

我將它上載到Azure管理門戶的雲服務>證書中。我把它作爲pfx上傳。 如果我將它轉換爲cer,它就會泄露私有密鑰,從我的理解中...(這可能是問題所在)。 請注意,我在Azure管理門戶和代碼中通過PEM嘗試了證書。這兩種情況都不起作用...... – sven

回答

0

我找到了解決方案。

創建一個x509Certificate並使用Import方法導入p12證書文件。

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

而現在它的工作原理...