2

I am using C# rest API with WebAPI 2.0; There are very few requests generating this exception. Find the below details:

.net version: 4.0 Stripe.net version: 34.20.0.0 Exception log:

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)

Things I have tried:

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 Create charge code:

 stripeCharge = chargeService.Create(myCharge);

I cannot upgrade the .net version because of the production dependency. Any help would be appreciated.

2 Answers2

3

I think you have the solution here, not the voted answer, but the one just below it:

TLS 1.2 in .NET Framework 4.0

Basically, you should add this line at application startup, and make sure .net4.5 is installed on the target machine:

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

*STRIPE payment gateway only supports TLS 1.1, TLS 1.2.

Hintee
  • 428
  • 3
  • 12
  • Thanks for the answer. I have already tried this as mentioned in the question. I cannot upgrade the .net version as mentioned in the question. – sandipchandanshive Mar 04 '20 at 05:38
  • Oh, my understanding was that you cannot upgrade the target version of the VS solution... – Hintee Mar 04 '20 at 15:19
  • 2
    But I don't think you have a workaround here, since .NET 4.0 does not support TLS 1.1 or 1.2. So your only option is to upgrade to .NET 4.5 on the target machine... – Hintee Mar 04 '20 at 15:22
0

If your OS is old and does not support TLS you have to upgrade OS too. Then code should be upgraded to .net 4.5 or higher. If you cannot upgrade .net framework to 4.5 I have more complex solution for you. Convert code to ASp.net core use self contained deployment. https://gunnarpeipman.com/visual-studio-publish-self-contained-aspnet-core-azure-appservice/

Create a .net core solution it can run in its Publish self-contained container. https://learn.microsoft.com/en-us/dotnet/core/deploying/

so you have .net 4.5 solution with operating system with no .net 4.5. Publish self-contained Publishing your app as self-contained produces a platform-specific executable. The output publishing folder contains all components of the app, including the .NET Core libraries and target runtime. The app is isolated from other .NET Core apps and doesn't use a locally installed shared runtime. The user of your app isn't required to download and install .NET Core.

The executable binary is produced for the specified target platform. For example, if you have an app named word_reader, and you publish a self-contained executable for Windows, a word_reader.exe file is created. Publishing for Linux or macOS, a word_reader file is created. The target platform and architecture is specified with the -r parameter for the dotnet publish command. For more information about RIDs, see .NET Core RID Catalog.

If the app has platform-specific dependencies, such as a NuGet package containing platform-specific dependencies, these are copied to the publish folder along with the app.

Jin Thakur
  • 2,711
  • 18
  • 15