2017-05-24 40 views
0

我已經創建了一個Asp.net Web應用程序-C#,我需要發送帶有.pfx證書的HttpWebRequest。將HttpWebRequest的.pfx證書添加到Web應用程序失敗

我已經將證書安裝到了個人證書文件夾(爲用戶NETWORK SERVICE增加了權限),並且能夠在X509Certificate2Collection中看到它,並且在將證書添加到HttpWebRequest時沒有問題。

但它拋出,而獲取的響應(HttpWebResponse)req.GetResponse()

我面對只能從Web應用程序這個問題不能連接到服務器錯誤,相同的代碼工作正常(得到了正確的響應)時,我嘗試使用C#控制檯應用程序。

  X509Store oLocalMachineStore = new X509Store(StoreName.My, StoreLocation.LocalMachine); 
      X509Certificate2Collection oCertColl; 
      X509Certificate oCert = new X509Certificate(); 
      oLocalMachineStore.Open(System.Security.Cryptography.X509Certificates.OpenFlags.ReadOnly); 
      oCertColl = oLocalMachineStore.Certificates.Find(System.Security.Cryptography.X509Certificates.X509FindType.FindByIssuerName, "mycertificate issuer", false); 
      oLocalMachineStore.Close(); 
      foreach (X509Certificate oCertFromStore in oCertColl) 
      { 
       if (oCertFromStore.Issuer.Contains("mycertificate issuer")) 
       { 
        oCert = oCertFromStore; 
        oCert.Import(@"certlocation\mycertificate.pfx", "pwd", X509KeyStorageFlags.MachineKeySet); 
        break; 
       } 
      } 

      HttpWebRequest req = (HttpWebRequest)WebRequest.Create("destination URL"); 
      req.ClientCertificates.Add(oCert); 

      req.UserAgent = "LOL API Client"; 
      req.Accept = "application/json"; 
      req.Method = WebRequestMethods.Http.Get; 

      string result = null; 
      using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) 
      { 
       StreamReader reader = new StreamReader(resp.GetResponseStream()); 
       result = reader.ReadToEnd(); 
      } 
+0

你在[Fiddler](http://www.telerik.com/fiddler)中看到了什麼? – AEonAX

+0

我已將應用程序池標識更改爲NetworkService,並添加了對NetworkService證書的完全控制。 –

+0

爲什麼你在循環中重新導入?導入一次並始終使用它們? – AEonAX

回答

0

將代理添加到http請求後問題得到解決。

req.Proxy = New Net.WebProxy("ProxyURL", False); 

但我仍然不知道,當我從出增加代理一個控制檯應用程序相同的代碼嘗試它是如何工作的。

如果有人對此有任何意見,請分享。

相關問題