简体   繁体   English

获取Paypal响应TLS 1.2 .Net Framework 4.0

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

We have a website that uses .Net Framework 4.0 and IIS 7. After the lasts updates of Paypal our IPN handler is not working any more. 我们有一个使用.Net Framework 4.0和IIS 7的网站。在Paypal的最新更新之后,我们的IPN处理程序不再起作用。 We had this error: The request was aborted: Could not create SSL/TLS secure channel sandbox account So as we are using .Net Framework 4.0 We added the next line: 我们遇到了此错误: 请求已中止:无法创建SSL / TLS安全通道沙箱帐户因此,在使用.Net Framework 4.0时,我们添加了下一行:

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

Now we have another error: The underlying connection was closed: An unexpected error occurred on a send. 现在,我们还有另一个错误:基础连接已关闭:发送中发生意外错误。

The class that handles the response is based in this example: https://mvcsamples.svn.codeplex.com/svn/trunk/Kona.Web/Controllers/PayPalController.cs 此示例中处理响应的类基于: https : //mvcsamples.svn.codeplex.com/svn/trunk/Kona.Web/Controllers/PayPalController.cs

After adding the before line now looks like this: 添加前一行之后,现在看起来像这样:

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;
 }

The exception is raised in this line: 此行中引发了异常:

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

We are not sure if we could make Paypal work with .NET Framework 4.0. 我们不确定是否可以使Paypal与.NET Framework 4.0一起使用。 There is said that is possible using the line we added. 据说可以使用我们添加的行。 But is not working. 但是没有用。 Maybe there are more changes needed to be done in the code or in the IS 7. But we couldn't find examples. 可能需要在代码或IS 7中进行更多更改。但是我们找不到示例。

Thank you for the help. 感谢您的帮助。

EDIT: 编辑:

I almost forgot to mention that the project is developed using Visual Studio 2010. Where there's not the .NET Framework 4.5. 我几乎忘了提到该项目是使用Visual Studio 2010开发的。在没有.NET Framework 4.5的地方。

EDIT 2: 编辑2:

I investigated the issue and I found that our server Windows Server 2008 didn't support TLS 1.2 (look at the table): https://blogs.msdn.microsoft.com/kaushal/2011/10/02/support-for-ssltls-protocols-on-windows/ But after that I found a new update of this year 2016 for all versions of SQL Server. 我调查了此问题,发现我们的服务器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/ 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 /

But at the end my boss for some reasons decided not to update the server using the new update or using the register method. 但是最后,由于某些原因,我的老板决定不使用新的更新程序或使用register方法来更新服务器。

So I can't prove any solution tosolve the problem. 因此,我无法证明任何解决方案。 Sorry for the trouble. 抱歉,添麻烦了。

I recently ran into a similar problem with Salesforce. 最近,我在Salesforce中遇到了类似的问题。 They are disabling Tls 1.0 support. 他们正在禁用Tls 1.0支持。

If you upgrade to .net 4.5, you can just add this code before you call 如果您升级到.net 4.5,则可以在致电之前添加此代码

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

If you can't upgrade to .net 4.5, you can set some registry values. 如果无法升级到.net 4.5,则可以设置一些注册表值。 Here's the info from https://help.salesforce.com/apex/HTViewSolution?id=000221207 这是来自https://help.salesforce.com/apex/HTViewSolution?id=000221207的信息

.NET 4.0 does not enable TLS 1.2 by default. 默认情况下,.NET 4.0不启用TLS 1.2。 To enable TLS 1.2 by default, it is possible to set the SchUseStrongCrypto DWORD value in the following two registry keys to 1, creating them if they don't exist: "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft.NETFramework\\v4.0.30319" and "HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft.NETFramework\\v4.0.30319". 默认情况下,要启用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”。 Those registry keys, however, will enable TLS 1.2 by default in all installed .NET 4.0, 4.5, 4.5.1, and 4.5.2 applications on that system. 但是,这些注册表项将默认在该系统上所有已安装的.NET 4.0、4.5、4.5.1和4.5.2应用程序中启用TLS 1.2。 We recommend testing this change before deploying it to your production servers. 我们建议先测试此更改,然后再将其部署到生产服务器中。 This is also available as a registry import file. 这也可以作为注册表导入文件使用。 These registry values, however, will not affect .NET applications that set the System.Net.ServicePointManager.SecurityProtocol value. 但是,这些注册表值不会影响设置System.Net.ServicePointManager.SecurityProtocol值的.NET应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM