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

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

Hi I am using c# sample code and getting error my code

 

 

string requestURL = ConfigurationManager.AppSettings["visaUrl"] + path;
string userId = ConfigurationManager.AppSettings["userId"];
string password = ConfigurationManager.AppSettings["password"];
string certificatePath = ConfigurationManager.AppSettings["cert"];
string certificatePassword = ConfigurationManager.AppSettings["certPassword"];
string statusCode = "";

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3 | (SecurityProtocolType)3072;

// Create the POST request object
HttpWebRequest request = WebRequest.Create(requestURL) as HttpWebRequest;
request.Method = method;

if (headers != null)
{
foreach (KeyValuePair<string, string> header in headers)
{
request.Headers[header.Key] = header.Value;
}
}

// Add headers
request.Headers["Authorization"] = GetBasicAuthHeader(userId, password);
request.Headers["ex-correlation-id"] = GetCorrelationId();
// Add certificate
var certificate = new X509Certificate2(certificatePath, certificatePassword);
request.ClientCertificates.Add(certificate);

 

string json = string.Empty;
if (request.Method == "POST")
{
var data = Encoding.ASCII.GetBytes(requestBodyString);
request.ContentType = "application/json";
request.Accept = "application/json";

using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
json = reader.ReadToEnd();
}
}
catch (System.Net.WebException webEx)
{

StreamReader sr = new StreamReader(webEx.Response.GetResponseStream());
string str = sr.ReadToEnd();

if (webEx.Status == WebExceptionStatus.ProtocolError)
{
int code = (int)((HttpWebResponse)webEx.Response).StatusCode;
if (code == 501)
{
var resp = new StreamReader(webEx.Response.GetResponseStream()).ReadToEnd();
return resp;
}
}
return webEx.Message;
}
catch (Exception ex)
{

return ex.Message;
}
return statusCode;


Error comes at 

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())

 

When i comment 
//var certificate = new X509Certificate2(certificatePath, certificatePassword);
//request.ClientCertificates.Add(certificate);
then its give me error 
<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY>
An error occurred while processing your request.<p>
Reference&#32;&#35;179&#46;87b61160&#46;1589380532&#46;736ec06
</BODY></HTML>

when I tried with postman I am able to hit it but response is in XML format

Capture.PNG

I have generated my certificate with the help of vdpplayground and i am able to get a response

but with my c# code, I am not able to get a response 
please help me 

 

16 REPLIES 16
API_Products
Visa Developer Support Specialist

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

Hey @granular-smart,

 

I've logged INC9456843 for the engineering team to investigate. We're currently working to resolve your issue and someone will get back to you soon. Feel free to reach out to us if you have other questions.

 

 




Thanks,

Diana H.



Was your question answered? Don't forget to click on "Accept as Solution" to help other devs find the answer to the same question.

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

Hi,
Waiting for your response

ricardo_visa
Community Scholar

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

Hey @granular-smart - I''m very sorry about this long wait. I have escalated this and our engineering team is on it. I personally am working to get you some additional sample code soon. Please let me know if there is anything else I can help with you in the meantime. 

 

 




Was your question answered? Don't forget to click on "Accept as Solution" to help other devs find the answer to the same question.
vivek241
Visa Employee

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

Hi granular-smart,

 

Can you log (Console.WriteLine) the response headers as well?

 

Refer example here - https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.headers?view=netcore-3.1

 

Once you log the response headers you should see a header with the name 'X-CORRELATION-ID'. If you do can you please share that header value?

 

Regarding the XML response in Postman - can you pass the 'Accept' header as 'application/json'.

 

Passing that header should return a JSON response instead of XML.

vivek241
Visa Employee

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

Hi @granular-smart ,

 

For C# you need to generate a .p12 file (PKCS12 keystore) using your private key and certificate.

You can generate it using the below openssl command

openssl pkcs12 -export -out keyandcertificate.p12 -inkey key.pem -in cert.pem

 

The following C# code works for me.

 

using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using System.Text;

public class HelloWorld {
    public static void Main (string[] args) {
        string requestURL = "https://sandbox.api.visa.com/vdp/helloworld";

        // TODO: put your userID from VDP here
        string userID = "";

        // TODO: put your password here
        string password = "";

        /**
        Using you private key (key.pem) and certificate (cert.pem) generate a p12 keystore using the below openssl command.
        openssl pkcs12 -export -out keyandcertificate.p12 -inkey key.pem -in cert.pem
        Add a password. Put the same password as the variable 'certificatePassword' below
        **/

        // TODO: put path to .p12 file here (e.g. /Users/vivek/Downloads/keyandcertificate.p12)
        string certificatePath = "";

        HttpWebRequest request = WebRequest.Create (requestURL) as HttpWebRequest;
        request.Accept = "application/json";
        request.Headers["Authorization"] = GetBasicAuthHeader (userID, password);

        var certificatePassword = "1234";
        var certificate = new X509Certificate2 (certificatePath, certificatePassword);
        request.ClientCertificates.Add (certificate);

        Console.WriteLine ("Making GET request to " + requestURL);
        HttpWebResponse httpWebResponse = (HttpWebResponse) request.GetResponse ();
        var responseBody = new StreamReader (httpWebResponse.GetResponseStream ()).ReadToEnd ();
        Console.WriteLine ("Response status: " + httpWebResponse.StatusCode);
        Console.WriteLine (responseBody);
    }

    private static string GetBasicAuthHeader (string userId, string password) {
        string authString = userId + ":" + password;
        var authStringBytes = Encoding.UTF8.GetBytes (authString);
        string authHeaderString = Convert.ToBase64String (authStringBytes);
        return "Basic " + authHeaderString;
    }
}

 Please try it out and let me know if you face any issues.

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

Hi Vivek,
I tried with the same code as well but getting the same error,
Is there any way so I can verify my certificate is it fine or not.
Capture.PNG

vivek241
Visa Employee

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

Hi @granular-smart ,

 

Can you print all the response headers and share the values?

 

Sample code below

try {
            Console.WriteLine ("Making {0} request to URL: {1}", request.Method, requestURL);
            HttpWebResponse httpWebResponse = (HttpWebResponse) request.GetResponse ();
            var responseBody = new StreamReader (httpWebResponse.GetResponseStream ()).ReadToEnd ();
            Console.WriteLine ("Response status: " + httpWebResponse.StatusCode);
            Console.WriteLine (responseBody);
        } catch (WebException webException) {
            HttpWebResponse httpWebResponse = (HttpWebResponse) webException.Response;
            Console.WriteLine ("Received error status code: " + httpWebResponse.StatusCode);
            Console.WriteLine ();
            Console.WriteLine ("The following headers were received in the response:");
            for (int i = 0; i < httpWebResponse.Headers.Count; ++i) {
                Console.WriteLine ("Header Name: {0}, Value: {1}", httpWebResponse.Headers.Keys[i], httpWebResponse.Headers[i]);
            }
        }

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

Hi Vivek,

I am not getting any response in exception 

it is giving  me an error at the line below in exception block.


HttpWebResponse httpWebResponse = (HttpWebResponse)webException.Response;
Console.WriteLine("Received error status code: " + httpWebResponse.StatusCode);

 

Capture.PNG

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

Hi,
Any luck