2

我正在使用带有 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; 
                    };

条纹创建收费代码:

 stripeCharge = chargeService.Create(myCharge);

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

4

2 回答 2

3

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

.NET Framework 4.0 中的 TLS 1.2

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

ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (安全协议类型)3072;

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

于 2020-03-03T21:17:51.863 回答
0

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

创建一个 .net 核心解决方案,它可以在其 Publish 自包含容器中运行。 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 包,则这些依赖项将与应用一起复制到发布文件夹。

于 2020-03-07T14:04:44.887 回答