2017-05-24 24 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); 

但我仍然不知道,当我从出增加代理一个控制台应用程序相同的代码尝试它是如何工作的。

如果有人对此有任何意见,请分享。

相关问题