2014-09-24 103 views
1

我有真正的問題,試圖聯繫一個安全的休息URL。基本上我對證書知之甚少,想知道我對證書文件所做的工作是否正確。.pem文件和.p12文件和URL的 - 哦,我的

  1. 我有2個URL有效地一個用於讀取和一個用於寫入。
  2. 我有2個.pem文件,每個都提供了這些URL。將它們導入證書管理器的行爲並未改變下面的結果)。
  3. 我也有一個.p12文件,如果我是誠實的,不知道它在哪裏適合所有這些...(我無法導入到我的Win7證書magager,因爲我沒有密碼)

當運行req.GetResponse()發生異常:

"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel." 
Inner Ex: "The remote certificate is invalid according to the validation procedure." 

我目前不確定的東西是否需要稍微修改一下或者我乾脆路要走。 代碼基本上...

if (!File.Exists(certificateLocation)) 
{ 
    throw new Exception(string.Format("The specified certificate file does not exist: {0}", certificateLocation)); 
} 

//Cert Challenge URL 
Uri requestURI = new Uri(url); 

//Create the Request Object 
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(requestURI); 

//Set the Request Object parameters 
req.ContentType = "application/x-www-form-urlencoded"; 
req.Method = "POST"; 
req.AllowAutoRedirect = false; 

//Create certificate from our file 
X509Certificate cert = X509Certificate.CreateFromCertFile(certificateLocation); 
req.ClientCertificates.Add(cert); 

WebResponse response = req.GetResponse(); // *** Errors here 

... 

編輯:目前,我只嘗試「讀」網址 - 似乎合乎邏輯。

發行人輸入密碼伴隨的.p12證書接觸,並導入到這一點認證管理器的「受信任的根證書頒發機構一節」後,錯誤已經改變以下...

System.Net.WebException 
"The underlying connection was closed: An unexpected error occurred on a receive." 
Inner Ex: 
"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host." 
+0

你從哪裏得到p12文件? iirc,密碼與文件同時創建。 – Robert 2014-09-24 10:33:59

+0

嗨羅伯特,p12來自發行人與url和pem文件同時發佈。 p12是否與pem文件一起使用?我可以不用p12嗎? – 2014-09-24 10:35:48

+1

對此有點生疏,但從我的無聊記憶,我認爲你會需要密碼。 – Robert 2014-09-24 10:40:16

回答

1

- 重新發布評論爲答案 -

此密碼是必需的,才能正常工作。

它與.p12文件同時創建。