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

Highlighted

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 

 

14 REPLIES 14
Highlighted
Visa Dev Moderator

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.

 

 



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

Thanks,
Diana

Highlighted

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

Hi,
Waiting for your response

Highlighted
Community Manager

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.
Highlighted
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.

Highlighted
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.

Highlighted

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

Hi Vivek,

I have created my .p12 file using the command as well as vdpplayground.

I am able to get response using postman but not with my code, and code is copied from sample code. sharing location where I am getting error.2020-05-25


ex-correlation-id -   74AL759X6J1F_SC
request body  -  {"acquirerCountryCode":"840","acquiringBin":"408999","amount":"124.02","businessApplicationId":"AA","cardAcceptor":{"address":{"country":"USA","county":"081","state":"CA","zipCode":"94404"},"idCode":"ABCD1234ABCD123","name":"Visa Inc. USA-Foster City","terminalId":"ABCD1234"},"cavv":"0700100038238906000013405823891061668252","foreignExchangeFeeTransaction":"11.99","localTransactionDateTime":"2020-05-13T06:03:29","retrievalReferenceNumber":"330000550000","senderCardExpiryDate":"2015-10","senderCurrencyCode":"USD","senderPrimaryAccountNumber":"4895142232120006","surcharge":"11.99","systemsTraceAuditNumber":"451001","nationalReimbursementFee":"11.22","cpsAuthorizationCharacteristicsIndicator":"Y","addressVerificationData":{"street":"XYZ St","postalCode":"12345"},"settlementServiceIndicator":"9","colombiaNationalServiceData":{"countryCodeNationalService":"170","nationalReimbursementFee":"20.00","nationalNetMiscAmountType":"A","nationalNetReimbursementFeeBaseAmount":"20.00","nationalNetMiscAmount":"10.00","addValueTaxReturn":"10.00","taxAmountConsumption":"10.00","addValueTaxAmount":"10.00","costTransactionIndicator":"0","emvTransactionIndicator":"1","nationalChargebackReason":"11"},"riskAssessmentData":{"delegatedAuthenticationIndicator":true,"lowValueExemptionIndicator":true,"traExemptionIndicator":true,"trustedMerchantExemptionIndicator":true,"scpExemptionIndicator":true},"visaMerchantIdentifier":"73625198"}

Anything else required, please let me know

 

 

Highlighted

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

Hi Vivek,

ex-correlation-id => GDSMO63GFLF7_SC
I have tried to create  .p12 file using the command as well as vdpplayground. I am getting a response using postman but not using code. 
if anything else required please let me know.

Highlighted

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

Highlighted
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]);
            }
        }