I've got a .Net 4.5 web api project that needs to send out an HttpWebRequest with a certificate. I'm able to get everything to work in Postman (
Postman | API Development Environment[
^]), but can't get my code to work. I keep getting this error: System.Net.WebException: 'The request was aborted: Could not create SSL/TLS secure channel.'
I've confirmed that my certificate is correct and I can see in debug that it is loading into the request object properly. I've even tried both load it from a pfx file and from the store.
When googling, there are a lot of results. Most seem to be fixed by specifying
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
or a version of it, using pipe symbols to include all protocols. It's worked for others because .Net 4.5 doesn't default to use TLS 1.2, so this is manually forcing it to be used. So I'm pretty sure I need it, but it didn't fix the error for me. :(
Also googled and found this doc,
TLS-SSL Settings | Microsoft Docs[
^], which talks about changing settings in the registry to enable Tls 1.2. I have also tried this, and it hasn't fixed it.
I'm at my wits end. Does anyone have any other ideas? If not on how to solve it, even to get more information about why it's failing?
Any help would be appreciated.
string json = JsonConvert.SerializeObject(objCustomObject);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.Expect100Continue = true;
ServicePointManager.DefaultConnectionLimit = 9999;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestPath);
request.Method = "POST";
request.ContentType = "application/json";
X509Store store = new X509Store(StoreName.My,StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection collection = store.Certificates.Find(X509FindType.FindBySubjectName, "mycertsubjectname",false);
request.ClientCertificates = collection;
request.Headers.Add("CUSTOM-HEADER-1", "headerValue1");
request.Headers.Add("CUSTOM-UNIX-EPOCH-TIMESTAMP-HEADER", "1557515741");
request.Headers.Add("CUSTOM-HEADER-3", "headerValue3");
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(json);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
What I have tried:
Tried adding this an some other Service Point Manager stuff.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Tried changing registry settings per instructions from
TLS-SSL Settings | Microsoft Docs[
^].
Tried different techniques adding the certificate.
A coworker tried converting our .Net 4.5 project to a .Net 4.6 project...didn't work.
Tried running VS as admin.
Tried adding permissions to the certificate in the cert manager.