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.