0

我們有一個使用.Net Framework 4.0和IIS 7的網站。 在Paypal的最後更新之後,我們的IPN處理程序不再工作。因爲我們使用的是.NET Framework 4.0中 The request was aborted: Could not create SSL/TLS secure channel sandbox account 所以我們加入下一行:我們有這個錯誤獲取貝寶迴應TLS 1.2 .Net Framework 4.0

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; // SecurityProtocolType.Tls12 

現在我們有另一個錯誤: 基礎連接已關閉:上一個發送發生意外的錯誤。

處理該響應的類是基於下面的例子: https://mvcsamples.svn.codeplex.com/svn/trunk/Kona.Web/Controllers/PayPalController.cs

現在前行加入後看起來是這樣的:

private string GetPayPalResponse(Dictionary<string, string> formVals, bool useSandbox) 
{ 
     string paypalUrl = useSandbox ? "https://www.sandbox.paypal.com/cgi-bin/webscr" 
      : "https://www.paypal.com/cgi-bin/webscr"; 

     ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; // SecurityProtocolType.Tls12 

     HttpWebRequest req = (HttpWebRequest)WebRequest.Create(paypalUrl); 

     // Set values for the request back 
     req.Method = "POST"; 
     req.ContentType = "application/x-www-form-urlencoded"; 

     byte[] param = Request.BinaryRead(Request.ContentLength); 
     string strRequest = Encoding.ASCII.GetString(param); 

     StringBuilder sb = new StringBuilder(); 
     sb.Append(strRequest); 

     foreach (string key in formVals.Keys) 
     { 
      sb.AppendFormat("&{0}={1}", key, formVals[key]); 
     } 
     strRequest += sb.ToString(); 
     req.ContentLength = strRequest.Length; 

     //for proxy 
     //WebProxy proxy = new WebProxy(new Uri("http://urlort#"); 
     //req.Proxy = proxy; 
     //Send the request to PayPal and get the response 
     string response = ""; 
     using (StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII)) 
     { 
      streamOut.Write(strRequest); 
      streamOut.Close(); linea = 10; 
      using (StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream())) 
      { 
       response = streamIn.ReadToEnd(); 
      } 
     } 
     return response; 
} 

唯一的例外是在此行中提出:

using (StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII)) 

我們不確定我們是否可以使用.NET Framework 4.0進行PayPal工作。據說可以使用我們添加的行。但沒有工作。也許在代碼或IS 7中需要做更多的更改,但我們找不到例子。

謝謝你的幫助。

編輯:

我差點忘了提,該項目使用Visual Studio 2010中哪裏有沒有在.NET Framework 4.5開發的。

編輯2:

我調查的問題,我發現,我們的服務器Windows Server 2008不支持TLS 1.2(看錶): https://blogs.msdn.microsoft.com/kaushal/2011/10/02/support-for-ssltls-protocols-on-windows/ 但在那之後,我發現這個新的更新2016年所有版本的SQL Server。 https://blogs.msdn.microsoft.com/sqlreleaseservices/tls-1-2-support-for-sql-server-2008-2008-r2-2012-and-2014/ http://blogs.sqlsentry.com/aaronbertrand/tls-1-2-support-read-first/

但最後我的老闆因某些原因決定不更新使用新的更新或使用寄存器方法服務器。

所以我不能證明任何解決方案解決問題。抱歉,添麻煩了。

+0

根據此頁 [鏈接](https://msdn.microsoft.com/it-it/library/system.net.securityprotocoltype(v = vs.100).aspx) TLS 1.2和TLS 1.1是在Framework 4.0中不受支持 – FabioThorin

+0

您是否找到針對此問題的解決方案?我們使用.NET 4.6.1,因此指定Tls |並不是問題Tls11 | Tls12用於ServicePointManager.SecurityProtocol。在Tls12設置後,我正在處理第二個異常「底層連接已關閉」。白日虛耗。無論我嘗試什麼,我都會得到這個例外。顯然還有其他的東西需要設置或配置,但是什麼? – Alex

回答

0

我最近遇到了與Salesforce類似的問題。他們正在禁用Tls 1.0支持。

如果升級到.NET 4.5,你可以添加此代碼打電話之前

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

如果您無法升級到.net 4.5,您可以設置一些註冊表值。以下是來自https://help.salesforce.com/apex/HTViewSolution?id=000221207的信息

.NET 4.0默認不啓用TLS 1.2。要默認啓用TLS 1.2,可以將以下兩個註冊表項中的SchUseStrongCrypto DWORD值設置爲1,如果它們不存在,則創建它們:「HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft.NETFramework \ v4.0.30319」和「HKEY_LOCAL_MACHINE \ SOFTWARE \ Wow6432Node \ Microsoft.NETFramework \ v4.0.30319" 。但是,這些註冊表項將啓用TLS 1。默認情況下,該系統上安裝的所有.NET 4.0,4.5,4.5.1和4.5.2應用程序均爲2。我們建議在將其部署到生產服務器之前對其進行測試。這也可用作註冊表導入文件。但是,這些註冊表值不會影響設置System.Net.ServicePointManager.SecurityProtocol值的.NET應用程序。

+0

我編輯了我的帖子,但澄清。我試圖在.NET 4.5項目中運行該問題,發現TLS 1.2或TLS 1.1都不起作用。所以問題出在服務器上而不是在應用程序中。 – ajmena