Calling Visa API from .NET results in error.

visadeveloper
Regular Visitor

Calling Visa API from .NET results in error.

Hi,

I am trying to call "helloworld" API using C#.NET. I am using C# code example downloaded from developer portal.

I downloaded the CSR and the Key and generated the .p12 file from them and replaced all values required for two-way SSL connection in the C# code. 

Following is my code.

public string MakeHelloWorldCall()
        {
            string requestURL = "<a href="https://sandbox.api.visa.com/vdp/helloworld" target="_blank">https://sandbox.api.visa.com/vdp/helloworld</a>";

            string userId = ConfigurationManager.AppSettings["userId"];
            string password = ConfigurationManager.AppSettings["password"];
            string certificatePath = ConfigurationManager.AppSettings["cert"];
            string certificatePassword = ConfigurationManager.AppSettings["certPassword"];
            string statusCode = "";
            HttpWebRequest request = WebRequest.Create(requestURL) as HttpWebRequest;
            request.Method = "GET";

            request.Headers["Authorization"] = GetBasicAuthHeader(userId, password);

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

            try
            {
                // Make the call
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    statusCode = response.StatusCode.ToString();
                }
            }
            catch (WebException e)
            {
                Console.WriteLine(e.Message);
                Exception ex = e.InnerException;
                while(ex != null)
                {
                    Console.WriteLine(ex.Message);
                    ex = ex.InnerException;
                }
                if (e.Response is HttpWebResponse)
                {
                    HttpWebResponse response = (HttpWebResponse)e.Response;
                    statusCode = response.StatusCode.ToString();
                }
            }
            return statusCode;
        }

 

I get following error at "request.GetResponse()".

The SSL connection could not be established, see inner exception. The credentials supplied to the package were not recognized

 

I have triple verified that UserId, Password, Certificate File Path and CertificatePassword are correctly supplied to the code.

This is C# code targeting .NET Core 2.1 running on Windows 10 Machine.

 

I was able to call "helloworld" api from SOAP UI successfully using the same certificate, userId and password.

 

Any help towards resolving this error would be greatly appreciated.

 

Thanks and regards,

Chetan Ranpariya

3 REPLIES 3
API_Products
Visa Developer Support Specialist

Re: Calling Visa API from .NET results in error.

Hi @visadeveloper,

 

Can you please let me know which API you are trying to call? Can you share your request header and snapshots of the error? 

 

Please try to resolve the issue by creating new .p12 using this command line
 
command line:
openssl pkcs12 -export -out p12certfile.p12 -inkey example-key.pem -in cert.pem

 




Thanks,

Diana



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

visadeveloper
Regular Visitor

Re: Calling Visa API from .NET results in error.

Hi,

Thanks for checking on this.

 

I started with calling helloworld API from sandbox environment. "vdp/helloworld".

 

I have already created the certificate file ".p12" file using the openssl command you suggested.

 

Actually I was able to resolve the issue. Instead of using certificate file (.p12) in the code, I installed the certificate on the machine and let the Framework code load the certificate from certificate store and use it for API call.

 

With the above approach I was able to call the APIs successfully. Following is my final code which is working.

 

 

 

 

public string RequestHelloWorld()
        {
            string requestURL = "vdp/helloworld";

            string userId = ConfigurationManager.AppSettings["userId"];
            string password = ConfigurationManager.AppSettings["password"];
            string apiResponse = "";

            try
            {
                return PerformDualAuthCall(requestURL, HttpMethod.Get, null, null);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return apiResponse;
        }

public string PerformDualAuthCall(string requestPath, HttpMethod method, object requestBody, Dictionary headers = null)
        {
            string requestURL = ConfigurationManager.AppSettings["visaUrl"] + requestPath;
            string userId = ConfigurationManager.AppSettings["userId"];
            string password = ConfigurationManager.AppSettings["password"];
            string apiResponse = "";

            try
            {
                if (headers == null)
                {
                    headers = new Dictionary();
                }

                headers.Add(HttpRequestHeader.Authorization.ToString(), GetBasicAuthHeader(userId, password));

                var httpClientHandler = new HttpClientHandler();
                httpClientHandler.ClientCertificateOptions = ClientCertificateOption.Automatic;
                using (HttpClient httpClient = new HttpClient(httpClientHandler))
                {

                    var request = new HttpRequestMessage
                    {
                        Method = method,
                        RequestUri = new Uri(requestURL)
                    };

                    if (headers != null)
                    {
                        foreach (var item in headers)
                        {
                            request.Headers.Add(item.Key, item.Value);
                        }
                    }

                    if (requestBody != null)
                    {
                        request.Content = new StringContent(requestBody is string ? requestBody as string : JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json");
                    };


                    // Make the call
                    var response = httpClient.SendAsync(request).Result;

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        apiResponse = response.Content.ReadAsStringAsync().Result;
                    }
                }
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine(e.Message);
            }
            return apiResponse;
        }

 

 

 

 

Please advise me if this is a one of the suggested ways to integrate with VISA APIs.

 

Thanks and regards,

Chetan Ranpariya

API_Products
Visa Developer Support Specialist

Re: Calling Visa API from .NET results in error.

Hi @visadeveloper,

 

Thanks for sharing with the VDC community on how you have resolved the issue! Glad to hear it works and that your getting a successful response which shows that your request is correct.

 




Thanks,

Diana



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