Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm making a request to an API, and I'm getting this error:

"Inner: Authentication failed because the remote party sent a TLS alert: 'HandshakeFailure.
Error: The SSL connection could not be established, see inner exception."

I am working on a Windows Server 2012 R2.

The funny thing is that I installed postman and in it I have no problem to receive the API response.

This is my C# code:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;

try
{
    var client = new HttpClient();

    var request = new HttpRequestMessage(HttpMethod.Post, "url...");
    var collection = new List<KeyValuePair<string, string>>();
    collection.Add(new("grant_type", "password"));
    collection.Add(new("username", "..."));
    collection.Add(new("password", "."));
    var content = new FormUrlEncodedContent(collection);
    request.Content = content;
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    var response = await client.SendAsync(request);
    response.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
    Console.WriteLine($"Inner: {ex.InnerException.Message}");
    Console.WriteLine($"Error: {ex.Message}");
}



I appreciate any help in advance!

What I have tried:

I tried adding this line
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;

As I saw suggested in several forums but without success.

I also tried to upgrade the .net version, I am currently using net7.
Posted
Comments
DaveDev 15-Apr-24 18:59pm    
Were you able to figure this out?
I have accessed an API for many years from Windows Server 2012 R2. The client was in framework 4.8 and another version is .NET 6. Both started failing on March 16th.
I have spent hours on this updating registry entries, updating group policy, and updating .NET along with various combination of ServicePointManager.SecurityProtocol settings.
Everything works on my Win11 dev machine. I tried installing Postman on the server and was had success so it seems like it's related to .NET.
clarksnake yesterday    
Did you find any solution?
In my case I realized that on my PC with Windows 11 it works correctly through localhost, but when publishing it on Windows Server 2012 I get the same error, in some forum I found that it is due to the Windows version and that it must be updated to at least 2016 , but I hope there is another more economical solution

1 solution

What you haven't told us is what you are running this code from. If you are running it on Windows 11 then, chances are, you have TLS13 enabled. This is not supported on Windows Server 2012 R2. To test this, remove the ServicePointManager code, and set the following registry key value to 0.
HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Client\Enabled
 
Share this answer
 
Comments
Fercap89 25-Mar-24 18:40pm    
My code is running on a Windows Server 12 R2 server and communicates with the code of an API, I do not know what operating system will have installed the API server.
I installed wireshark and it seems that the communication is being done in TLS 1.2.

Then I tried the same with Postman (which I have also installed on the windows server and it works) and the communication is done in TLS 1.2.

The chipers in both cases are the same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900