'Error in production but not in local dev machine: The request was aborted: Could not create SSL/TLS secure channel

I am encountering this error

When I turned on tracing using System.Diagnostics with verbose logs, I get this

System.Net Information: 0 : [11612] SecureChannel#16995117::.ctor(hostname=api.convergepay.com, #clientCertificates=0, encryptionPolicy=RequireEncryption)
System.Net Information: 0 : [11612] Enumerating security packages:
System.Net Information: 0 : [11612]     Negotiate
System.Net Information: 0 : [11612]     NegoExtender
System.Net Information: 0 : [11612]     Kerberos
System.Net Information: 0 : [11612]     NTLM
System.Net Information: 0 : [11612]     TSSSP
System.Net Information: 0 : [11612]     pku2u
System.Net Information: 0 : [11612]     WDigest
System.Net Information: 0 : [11612]     Schannel
System.Net Information: 0 : [11612]     Microsoft Unified Security Protocol Provider
System.Net Information: 0 : [11612]     CREDSSP
System.Net Information: 0 : [11612] SecureChannel#16995117 - Left with 0 client certificates to choose from.
System.Net Information: 0 : [11612] SecureChannel#16995117::.AcquireClientCredentials, new SecureCredential() (flags=(ValidateManual, NoDefaultCred, SendAuxRecord), m_ProtocolFlags=(Ssl3Client, Tls10Client), m_EncryptionPolicy=RequireEncryption)
System.Net Information: 0 : [11612] AcquireCredentialsHandle(package = Microsoft Unified Security Protocol Provider, intent  = Outbound, scc     = System.Net.SecureCredential)
System.Net Information: 0 : [11612] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = (null), targetName = api.convergepay.com, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [11612] InitializeSecurityContext(In-Buffer length=0, Out-Buffer length=135, returned code=ContinueNeeded).
System.Net Information: 0 : [11612] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = e27744e250:95834c9050, targetName = api.convergepay.com, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [11612] InitializeSecurityContext(In-Buffers count=2, Out-Buffer length=0, returned code=Unsupported).
System.Net Error: 0 : [11612] Exception in HttpWebRequest#15613775:: - The request was aborted: Could not create SSL/TLS secure channel..
System.Net Error: 0 : [11612] Exception in HttpWebRequest#15613775::EndGetRequestStream - The request was aborted: Could not create SSL/TLS secure channel..

I don't know what those logs mean but what I was was trying to do is in my asp.net page, I am making payment by sending a WebRequest like this

        HttpWebRequest objRequest = (HttpWebRequest) WebRequest.Create("https://api.convergepay.com/VirtualMerchant/processxml.do");
        objRequest.Referer = "myreferrerpage.aspx";
        objRequest.Method = "POST";
        objRequest.ContentLength = xmlFormattedPostString.Length;
        objRequest.ContentType = "application/x-www-form-urlencoded";

        using (StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream()))
        {
            myWriter.Write(xmlFormattedPostString);
            myWriter.Close(); 
        }

        // returned values are returned as a stream, then read into a string
        HttpWebResponse objResponse = (HttpWebResponse) objRequest.GetResponse();
        using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
        {
            post_response = responseStream.ReadToEnd();
            responseStream.Close();
        }
        ConvergeResult chargeResult = new ConvergeResult();

However, I am getting this error when sending the HttpWebRequest GetRequestStream.

This only happens when in production, but when I unit test the below code in my local machine, it is sending the request and getting a response successfully (an xml formatted response from the api).

For the application projects, I just upgraded it to .Net Framework 4.7.2.

The Windows version of my IIS is Windows Server 2012 R2 Datacenter

In my code also, I set the SecurityProtocol to be this:

        WebRequestHelper.UsingSecurityProtocol(System.Net.SecurityProtocolType.Tls12, () =>
        {
            using (StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream()))
            {
                myWriter.Write(xmlFormattedPostString);
                myWriter.Close();
            }
        });


    public static void UsingSecurityProtocol(SecurityProtocolType securityProtocolType, Action action)
    {
        lock (_lock)
        {
            SecurityProtocolType origProtocol = ServicePointManager.SecurityProtocol;
            System.Net.ServicePointManager.SecurityProtocol = securityProtocolType;
            //ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(AlwaysGoodCertificate);
            action();
            System.Net.ServicePointManager.SecurityProtocol = origProtocol;
        }
    }

Can someone give me some inputs as to how to resolve this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source