簡體   English   中英

獲取Paypal響應TLS 1.2 .Net Framework 4.0

[英]Get Paypal response TLS 1.2 .Net Framework 4.0

我們有一個使用.Net Framework 4.0和IIS 7的網站。在Paypal的最新更新之后,我們的IPN處理程序不再起作用。 我們遇到了此錯誤: 請求已中止:無法創建SSL / TLS安全通道沙箱帳戶因此,在使用.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))

我們不確定是否可以使Paypal與.NET Framework 4.0一起使用。 據說可以使用我們添加的行。 但是沒有用。 可能需要在代碼或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 /

但是最后,由於某些原因,我的老板決定不使用新的更新程序或使用register方法來更新服務器。

因此,我無法證明任何解決方案。 抱歉,添麻煩了。

最近,我在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”。 但是,這些注冊表項將默認在該系統上所有已安裝的.NET 4.0、4.5、4.5.1和4.5.2應用程序中啟用TLS 1.2。 我們建議先測試此更改,然后再將其部署到生產服務器中。 這也可以作為注冊表導入文件使用。 但是,這些注冊表值不會影響設置System.Net.ServicePointManager.SecurityProtocol值的.NET應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM