1

I am trying to do a POST request with data, however I get an Exception about the project could not create a secure channel SSL or TSL, when the function request.GetRequestStream() is reached.

This is the function that I use:

public bool PreRegister()
{
    try
    {
        string url = "https://paytest.megasoft.com.ve/payment/action/v2-preregistro";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Headers.Add("Authorization: Basic " + Crypt.Base64_Encode("grupototal99:Grupo01."));
        request.Method = "POST";
        request.ContentType = "application/xml";
        request.Accept = "application/xml";
        
        XElement requestXML =
            new XElement("request", new XElement("cod_afiliacion", 1911202001));

        byte[] bytes = Encoding.UTF8.GetBytes(requestXML.ToString());

        request.ContentLength = bytes.Length;

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;

        using (Stream putStream = request.GetRequestStream())
        {
            putStream.Write(bytes, 0, bytes.Length);
        }
        
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream resStream = response.GetResponseStream();
        StreamReader rdr = new StreamReader(resStream);
        code = rdr.ReadToEnd();
        return true;
    }
    catch(Exception ex)
    {
        return false;
    }
}
funie200
  • 3,688
  • 5
  • 21
  • 34
  • Does this answer your question? [HttpWebRequest: The request was aborted: Could not create SSL/TLS secure channel](https://stackoverflow.com/questions/36006333/httpwebrequest-the-request-was-aborted-could-not-create-ssl-tls-secure-channel) – funie200 Nov 23 '20 at 12:45
  • Remove all the options except TLS 1.2/1.3 : ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13; – jdweng Nov 23 '20 at 12:45
  • I added this lines of code to resolve it ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3; ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; – Edgar Gomez Nov 23 '20 at 12:52

1 Answers1

-1

I added this line of code from other users that have the same issue and resolve it

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
  • This code allows dangerously outdated encryption algorithms. Do not use it. Currently you'd usually _only_ allow TLS 1.2, as 1.1 and 1.0 (let alone SSL3!) are simply not secure enough. What's _probably_ going on here, is that the API only allows TLS 1.2 connections (given it's a payment gateway), and that you're on a .NET Framework version prior to 4.7.1, so you're connecting with TLS 1.1 or even 1.0. Update your server, update the framework, run TLS 1.2 automatically. If you can't, stick with `ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12` and lose the second line of code. – CodeCaster Nov 23 '20 at 15:44