I am trying to make a GET request to VISA api from .NET Core and I am getting the following error message in WebException
The credentials supplied to the package were not recognized visa
VISA APIs support two-way SSL connection and for that a set of private key and certificate are issued by visa along with a pair of userid and password.
I created a p12
certificate file using following command as per the VISA docoumentation.
openssl pkcs12 -export -in cert.pem -inkey "privateKey.pem"
-certfile cert.pem -out myProject_keyAndCertBundle.p12
I then wrote following code to make a request to VISA API. I am running this code as part of a .NET Core console application.
public string MakeHelloWorldCall()
{
string requestURL = "https://sandbox.api.visa.com/vdp/helloworld";
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
// This is the line which throws the exception.
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;
}
private string GetBasicAuthHeader(string userId, string password)
{
string authString = userId + ":" + password;
var authStringBytes = Encoding.UTF8.GetBytes(authString);
string authHeaderString = Convert.ToBase64String(authStringBytes);
return "Basic " + authHeaderString;
}
I see following output in Console.
The SSL connection could not be established, see inner exception. The credentials supplied to the package were not recognized
The SSL connection could not be established, see inner exception.
The credentials supplied to the package were not recognized
So the root error is The credentials supplied to the package were not recognized
.
I am pretty much stuck here as I am not sure what is causing this error.
I tried searching about this error on Google and SO. But most of the posts suspects insufficient privilege to access the certificate. But I am running Visual Studio with Administrator User account and also I am using certificate file not the certificate installed on the machine.
I guess anyone with experience with Two-Way SSL Connection and/or VISA APIs would be able to understand what exact reason behind this error.
EDIT: I am able to call the API successfully from SOAP UI where I configured the same .p12
certificate and Authorization header.