【问题标题】:Stripe Payment: System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel条纹支付:System.Net.WebException:请求被中止:无法创建 SSL/TLS 安全通道
【发布时间】:2020-02-28 04:48:50
【问题描述】:

我正在使用带有 WebAPI 2.0 的 C# REST API;生成此异常的请求很少。查找以下详细信息:

.net 版本:4.0 Stripe.net 版本:34.20.0.0 异常日志:

2020-02-18 06:47:45.4533|DEBUG|Services.impl.StripePaymentChargeService|System.Net.WebException: The 

request was aborted: Could not create SSL/TLS secure channel.
   at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
   at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
2020-02-18 06:47:45.4533|DEBUG|Services.impl.StripePaymentChargeService|   at Stripe.SystemNetHttpClient.<MakeRequestAsync>d__21.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Stripe.StripeClient.<RequestAsync>d__25`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Stripe.Service`1.<RequestAsync>d__24`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Stripe.Service`1.Request[T](HttpMethod method, String path, BaseOptions options, RequestOptions requestOptions)
   at Stripe.ChargeService.Create(ChargeCreateOptions options, RequestOptions requestOptions)

我尝试过的事情:

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



2.  ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

                System.Net.ServicePointManager.ServerCertificateValidationCallback +=
                        delegate (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                                               System.Security.Cryptography.X509Certificates.X509Chain chain,
                                               System.Net.Security.SslPolicyErrors sslPolicyErrors)
                        {
                            return true; 

                        };

3. ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

            System.Net.ServicePointManager.ServerCertificateValidationCallback +=
                    delegate (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                                           System.Security.Cryptography.X509Certificates.X509Chain chain,
                                           System.Net.Security.SslPolicyErrors sslPolicyErrors)
                    {
                        return true; 
                    };

Stripe 创建收费代码:

 stripeCharge = chargeService.Create(myCharge);

由于生产依赖性,我无法升级 .net 版本。 任何帮助将不胜感激。

【问题讨论】:

  • 也许stackoverflow.com/a/44153734/9769731 会有所帮助?
  • @karllekko 感谢您的评论,我没有编辑注册表的权限,必须在不升级的情况下找到解决方案。

标签: c# .net ssl stripe-payments webapi


【解决方案1】:

我认为您在这里有解决方案,不是投票的答案,而是它下面的答案:

TLS 1.2 in .NET Framework 4.0

基本上,您应该在应用程序启动时添加此行,并确保目标机器上安装了 .net4.5:

ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;

*STRIPE 支付网关仅支持 TLS 1.1、TLS 1.2。

【讨论】:

  • 感谢您的回答。如问题中所述,我已经尝试过此操作。我无法升级问题中提到的 .net 版本。
  • 哦,我的理解是不能升级目标版本的VS方案……
  • 但我认为您在这里没有解决方法,因为 .NET 4.0 不支持 TLS 1.1 或 1.2。所以你唯一的选择是在目标机器上升级到 .NET 4.5...
【解决方案2】:

如果您的操作系统较旧且不支持 TLS,您也必须升级操作系统。 然后代码应该升级到 .net 4.5 或更高版本。 如果您无法将 .net 框架升级到 4.5,我会为您提供更复杂的解决方案。 将代码转换为 ASp.net 核心使用自包含部署。 https://gunnarpeipman.com/visual-studio-publish-self-contained-aspnet-core-azure-appservice/

创建一个可以在其 Publish 独立容器中运行的 .net 核心解决方案。 https://docs.microsoft.com/en-us/dotnet/core/deploying/

所以你有 .net 4.5 解决方案,操作系统没有 .net 4.5。 发布自包含 将您的应用程序发布为独立的会生成特定于平台的可执行文件。输出发布文件夹包含应用程序的所有组件,包括 .NET Core 库和目标运行时。该应用程序与其他 .NET Core 应用程序隔离,不使用本地安装的共​​享运行时。您的应用用户无需下载和安装 .NET Core。

为指定的目标平台生成可执行二进制文件。例如,如果您有一个名为 word_reader 的应用程序,并且您发布了一个适用于 Windows 的自包含可执行文件,则会创建一个 word_reader.exe 文件。为 Linux 或 macOS 发布时,会创建一个 word_reader 文件。使用 dotnet publish 命令的 -r 参数指定目标平台和体系结构。有关 RID 的详细信息,请参阅 .NET Core RID 目录。

如果应用具有特定于平台的依赖项,例如包含特定于平台的依赖项的 NuGet 包,则这些依赖项会与应用一起复制到发布文件夹。

【讨论】:

    猜你喜欢
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 2018-04-22
    • 2012-10-30
    相关资源
    最近更新 更多