Twitter API responds with 'An existing connection was forcibly closed by the remote host'
Asked Answered
D

4

11

My company had some dotNET code which has been talking to Twitter's API happily over the last three months, tweeting our news out.

On 29th July it stopped working, and the POST request was hit with this error:
An existing connection was forcibly closed by the remote host
System.Net.Sockets.SocketException

I assume this is because our server is still using TLS 1.1 and I only just found this Twitter API announcement from June, which says:

Since the removal of SSL support 104 in 2014, the Twitter API has required a minimum of TLS 1.0 for all incoming connections. Both TLS 1.0 and 1.1 were superseded by TLS 1.2 in 2008.

Beginning July 25th 2019, all connections to the Twitter API (and all other Twitter domains) will require TLS 1.2. This change will affect all formats and tiers of the API (REST, streaming, and webhooks; standard, premium, enterprise, and Ads and Media APIs), as well as the wider Twitter platform.

It seems we got 4 days grace, as our code had no problems between 25th and 28th July.

I believe the server itself has TLS 1.2 installed, so perhaps the issue is that this project was compiled with dotNET 4.0 which defaults to using TLS 1.1?

Will upgrading our project to dotNET 4.5 solve the problem, or could it be something else?

Dylane answered 30/7, 2019 at 12:10 Comment(1)
It sounds like 4.6 might work automatically, else there's a flag you can set: Update .NET web service to use TLS 1.2Ossian
D
2

I came to install dotNET 4.5 and was given the options to repair/remove, indicating it was already installed! This was unexpected.

So I added this line of code:
System.Net.ServicePointManager.SecurityProtocol = 3072

And changed Web.config to use targetFramework="4.5"

Everything now working again. Thanks.

Dylane answered 31/7, 2019 at 13:32 Comment(0)
S
11

I had the same issue start on July 29th as well. My project is compiled with .NET 4.5.2 and uses the LinqToTwitter library. I was able to fix the problem by adding the following line prior to making Twitter calls.

System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12;
Simonesimoneau answered 31/7, 2019 at 4:19 Comment(0)
D
2

I came to install dotNET 4.5 and was given the options to repair/remove, indicating it was already installed! This was unexpected.

So I added this line of code:
System.Net.ServicePointManager.SecurityProtocol = 3072

And changed Web.config to use targetFramework="4.5"

Everything now working again. Thanks.

Dylane answered 31/7, 2019 at 13:32 Comment(0)
A
2

Just had the same issue myself. However, beware that the change to ServicePointManager will apply to all endpoints. So, if you are connecting to another that doesn't support TLS 1.2, it will stop working.

So, a better solution would be to or the Tls12 value with the existing value.

System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12;
Azzieb answered 31/7, 2019 at 22:47 Comment(0)
R
0

Same issue. The call was failing when Twitter was calling ExternalLogin. I assume it was a TLS 1.2 issue. It was failing here (see code below) as the User was not authenticated. Facebook Auth would not trigger a new ChallengeResult, so I knew this was how it was supposed to work.

if (!User.Identity.IsAuthenticated)
{
  return new ChallengeResult(provider, this);
} 

My Asp.Net Web API was already targeting 4.6.1, but the runtime was configured as 4.5. The fix for me was to update this line in the Web.config and rebuild the app.

<system.Web>
<httpRuntime targetFramework="4.6.1" />
</system.Web>

Once I made this change, the Twitter Auth window displayed once again.

NOTE: I've read elsewhere that you should not mess with the System.Net.ServicePointManager.SecurityProtocol setting. Asp.Net 4.6.1 defaults to TLS 1.2, but it is supposed to be backwards compatible with 1.1 and 1.0. I'm about to test with some Xamarin apps that use this API, so I will let everyone know what happens. Update coming!

Ramble answered 26/11, 2019 at 17:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.