Could not create SSL/TLS secure channel from soap c# SoapHttpClientProtocol

2.6k Views Asked by At

Cannot access https url from c# code. trying to access a https://blabla url and access the get method. this is SOAP generated c# class from wsdl. it works from SOAP UI without any problem but doesn't work from the code.i get the error as "Could not create SSL/TLS secure channel"

2

There are 2 best solutions below

3
On

I've had this so many times.

You need to set the SecurityProtocol before executing the method. The HTTP Request you are trying to make expects the request to be sent using Transport Layer Security.

To support TLS in your request, use the following code (this sets it to Tls 1.2) before firing the HTTP Request: System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

You can find more information on the different settings from this URL: https://learn.microsoft.com/en-us/dotnet/api/system.net.securityprotocoltype?view=netcore-3.0

If you find that this enum does not exist (because you are using an older version of .NET), you can use the enum integer value from the link above. This example supports Tls 1.2. System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)3072;

1
On

I'm not sure then, unless the following works. But you mentioned that you already tried setting the SecurityProtocol enum. It might be that your webservice connection expects to be supported with TLS 1.3.

Try the following code and uncomment each line that begins with System.Net.ServicePointManager.SecurityProtocol individually to see if it resolves your issue:

            // Uncomment one of these four to see if it works
            //System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
            //System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)3072;
            //System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls13;
            //System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)12288;

            using (var client = new WebService())
            {
                data = new JavaScriptSerializer().Deserialize<List<LookupModel>>(client.GetLookupDetails());
                TempData["lookupData"] = data;
            }