For testing purposes, I am trying to force my code to use an outdated version of TLS to trigger a particular API response.
However, despite TLS 1.0 shown as being used, the request succeeds. The endpoint used only allows for TLS 1.2.
using System.Net;
namespace MyNameSpace {
class myApp {
private static readonly HttpClient client = new HttpClient();
static void Main(string[] args) {
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls; // This is TLS 1.0
Console.WriteLine(ServicePointManager.SecurityProtocol);
MakeRequest();
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
}
public async static void MakeRequest() {
var responseString = await client.GetStringAsync("https://api.twilio.com:8443");
Console.WriteLine(responseString);
}
}
}
This code returns
>>> dotnet run
Tls
Press any key to exit...
<?xml version='1.0' encoding='UTF-8'?>
<TwilioResponse><Versions><Versions><Version><Name>2010-04-01</Name><Uri>/2010-04-01</Uri><SubresourceUris><Accounts>/2010-04-01/Accounts</Accounts></SubresourceUris></Version></Versions></Versions></TwilioResponse>
However, when forcing cURL to use TLS 1.0 the request fails as it should:
> curl -v https://api.twilio.com:8443 --tlsv1.0 --tls-max 1.1
* Trying 52.2.204.41...
* TCP_NODELAY set
* Connected to api.twilio.com (52.2.204.41) port 8443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/cert.pem
CApath: none
* TLSv1.1 (OUT), TLS handshake, Client hello (1):
* TLSv1.1 (IN), TLS alert, protocol version (582):
* error:1400442E:SSL routines:CONNECT_CR_SRVR_HELLO:tlsv1 alert protocol version
* Closing connection 0
curl: (35) error:1400442E:SSL routines:CONNECT_CR_SRVR_HELLO:tlsv1 alert protocol version
How can I force my code to use an OUTDATED version of TLS to trigger this error? Right now, my setting doesn't seem to stick.
NB: From what I can gather I am using .NET 6.0.1
Addendum: It was suggested to set the protocol before instantiating the HTTPClient. An even simpler example yields the same result, code below:
using System.Net;
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
HttpClient client = new HttpClient();
Console.WriteLine(ServicePointManager.SecurityProtocol);
var responseString = await client.GetStringAsync("https://api.twilio.com:8443");
Console.WriteLine(responseString);