The request was aborted: Could not create SSL/TLS secure channel

Abdou ysf 6 Reputation points
2022-11-10T09:52:35.673+00:00

Hi all,

I'm working on a project in visual studio 2022 using .NET framework 4.8, I want to call HttpWebRequest.

Whene I run the project from visual studio, it works fine. But once I deploy it to windows server 2019 IIS, I'm facing this error "System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel".

The code is as below for HttpWebRequest, and it is working without errors in my workstation. requestURL starting with "https".

    ServicePointManager.Expect100Continue = true;  
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;  
         
        HttpWebRequest request = WebRequest.Create(requestURL) as HttpWebRequest;  
        request.Method = "POST";  

        // Add headers  
        request.Headers["ID"] = Id;  

        // Add certificate  
        var certificate = new X509Certificate2(certificatePath, certificatePassword);  
        request.ClientCertificates.Add(certificate);  
        request.PreAuthenticate = true;  

        try  
        {  

            // Make the call  
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)  
            {  
                statusCode = response.StatusCode.ToString();  
                var res = response.GetResponseHeader("RESPONSE_STATUS");  
				return res;  
            }  
        }  
        catch (WebException e)  
        {  
            if (e.Response is HttpWebResponse)  
            {  
                HttpWebResponse response = (HttpWebResponse)e.Response;  
                statusCode = response.StatusCode.ToString();  
                return statusCode;  
            }  
            return e.ToString();  
        }  
Internet Information Services
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Sam Wu-MSFT 7,136 Reputation points Microsoft Vendor
    2022-11-14T03:22:36.927+00:00

    @Abdou ysf

    Usually what happens is the server requires the TLS protocol but the client targets a framework that does not support the TLS version. The first step is figuring out what version of TLS is required. then make sure the client supports the required TLS version.

    Transport Layer Security (TLS) best practices with the .NET Framework.

    Below links can help you set the TLS version in the windows server and client:

    How to enable TLS 1.2 on the site servers.

    How to enable TLS 1.2 on clients.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments