Could not create SSL/TLS secure channel .Net 4.8 - c#

Currently, I am working on .net 4.8-based console application and I want to check if an URL is alive or not. For some URLs, I am getting the "Could not create SSL/TLS secure channel" error. Please suggest me how can I resolve this. This error is not generating on my local PC, only generating on the server. My server OS is 2012R2
Please note that I have already tried the below options but these didn't work.
Option 1:
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Option 2:
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
Option 3:
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

Related

C# Webclient cannot download api.github.com pages

Visual Studios 2019 C# .NET Framework 4.6
using System.Net;
using System.Net.NetworkInformation;
WebClient wc = new WebClient();
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
wc.DownloadFile(new System.Uri("https://api.github.com/repos/[Username]/[Reponame]/releases/latest"), "name.json");
I want my app to check for updates from its own github repo but the above code is unable to download the latest release page from github api.
It is strange that I am able to download everything else including raw files from github using the same code as above but not pages from api.github.com
EDIT: The Github Repo I am trying this with is not private.
No compile time error. Below are some screenshots of debug runtime exceptions:
I figured out the problem. api.github.com need the User-Agent header in requests.
Just had to change the code to this: (Only added the fourth line)
WebClient wc = new WebClient();
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
client.Headers.Add ("user-agent", "request");
wc.DownloadFile(new System.Uri("https://api.github.com/repos/[Username]/[Reponame]/releases/latest"), "name.json");

Https leads to exception using HttpClient

I have a problem calling my ASP.NET Web API from a client application. Many questions regarding this topic were asked here but none of the answers solved my issue.
Problem:
When I try to call my API I get the following exception:
System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
Info:
Both projects (cleint and API) are based on .NET 4.5.2
In my test environment client and API are running on the same VM
Everything worked fine wihtout https
I created a selfsigned certificate for testing purposes but no browser shows a warning so it seems to be fine
Code:
HttpClient client = new HttpClient();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
client.BaseAddress = new Uri(apiBaseUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.PostAsJsonAsync("call/CreateNew", call);
I tried several solutions provided in the answers of similar questions, but none of these worked so far:
Using .NET Framework 4.6 instread of 4.5.2
Specified the protocol with ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
Tried ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; (just to be sure)
In another question someone suggested using Wireshark to further analyze the problem. I gave it a try but I have to admit that I did not really know what I was doing.
Anyway I found segments that said "ACKed segment that wasn't captured" and "Previous segment(s) not captured" of which the second contained data I actually tried to send to my API.
Can you give me some advice what to try next or how to fix this error?

C# Windows Service ,The request was aborted: Could not create SSL/TLS secure channel with SecurityProtocol

So We have windows service api that gets data then send to web using webclient
the problem is we have this certain branch that sends error while other branches api are working fine except for this one branch that sends error:
"The request was aborted: Could not create SSL/TLS secure channel"
i have added
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
yet the error still occured , please help and thankyou
heres the code
I'm guessing you already followed the top voted here: The request was aborted: Could not create SSL/TLS secure channel
Try to extend you tls protocols like this, so that it can fallback to previous versions if the server does not support them:
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
If it doesn't work still, try the answer by User:APW with extra troubleshooting steps.

.Net could not create SSL/TLS secure channel

I am posting xml from a .net application to a third party web service but receive a "could not create SSL/TLS secure channel" error. When I make the request with soapUI it works fine and i get a response. But cant seem to get it from my .net console app.
I have tried setting the security to tls1 and tls12 but still no success. The certificate is installed on the server from which i am making these requests.
Is there anyone who has managed to solve this issue?
Here is a sample of my code
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://thirdPartyURL/cgi-bin/XmlProc");
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes("myXML");
request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
request.Method = "POST";
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
Fixed this by doing the following:
Changing the .Net Framework to 4.5
Installing the certificate in the "Third-Party Root Authorities" store
Added this line of code before making the request in my app
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
Ran the application on a windows server 2012 instead of windows server 2008. It might be a case that windows server 2008 does not support TLS v1.2

How does ServicePointManager.SecurityProtocol work?

I would like to know how the property ServicePointManager.SecurityProtocol works when I set three different SecurityProtocolType on her flags. I.E:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
Will the communication try first to communicate with TLS, and if it fails try TLS1.2 and after SSL3?
If not, what do these flags mean and how does it work?
Whichever communication object you're using (HttpClient, HttpWebRequest, etc) will try to negotiate to the highest level possible first. Failing that it will keep going "down" the chain.
If you're using .Net 4.6 then the default security protocols will look like this because SSL3 is broken:
SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12
If you really need to use SSL3 for some reason and are using .Net 4.6, see this MS KB article on how to force it to be insecure: https://support.microsoft.com/en-us/kb/3069494
You might also be asking how this protocol level is actually determined? This is step 1 of the SSL handshake process where each end of the connection says "I support this version". Here is an interesting read on the full handshake process: http://www.truedigitalsecurity.com/blog/2015/05/20/ssltls-protocol-version-negotiation/

Categories