获得Paypal响应TLS 1.2 .Net Framework 4.0

时间:2016-06-07 11:36:12

标签: c# asp.net-mvc visual-studio-2010 paypal-ipn paypal-sandbox

我们有一个使用.Net Framework 4.0和IIS 7的网站。 在Paypal持续更新后,我们的IPN处理程序不再起作用了。我们遇到了这个错误: The request was aborted: Could not create SSL/TLS secure channel sandbox account 因此,当我们使用.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方法更新服务器。

所以我无法证明解决问题的任何解决方案。抱歉,麻烦。

1 个答案:

答案 0 :(得分: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,如果它们不存在则创建它们:&#34; HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft.NETFramework \ v4。 0.30319&#34;和&#34; HKEY_LOCAL_MACHINE \ SOFTWARE \ Wow6432Node \ Microsoft.NETFramework \ v4.0.30319&#34;。但是,这些注册表项将在该系统上的所有已安装的.NET 4.0,4.5,4.5.1和4.5.2应用程序中默认启用TLS 1.2。我们建议在将此更改部署到生产服务器之前对其进行测试。这也可用作注册表导入文件。但是,这些注册表值不会影响设置System.Net.ServicePointManager.SecurityProtocol值的.NET应用程序。