我正在尝试从 .NET Core 向 VISA api 发出 GET 请求,并且在 WebException 中收到以下错误消息
提供给包裹的凭据不被认可签证
VISA API 支持双向 SSL 连接,为此,visa 会颁发一组私钥和证书以及一对用户名和密码。
我p12
根据 VISA 文档使用以下命令创建了一个证书文件。
openssl pkcs12 -export -in cert.pem -inkey "privateKey.pem"
-certfile cert.pem -out myProject_keyAndCertBundle.p12
然后我编写了以下代码来向 VISA API 发出请求。我将此代码作为 .NET Core 控制台应用程序的一部分运行。
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;
}
我在控制台中看到以下输出。
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
所以根本错误是The credentials supplied to the package were not recognized
.
我几乎被困在这里,因为我不确定是什么导致了这个错误。
我尝试在 Google 和 SO 上搜索此错误。但大多数帖子都怀疑访问证书的权限不足。但是我正在使用管理员用户帐户运行 Visual Studio,而且我使用的是证书文件,而不是机器上安装的证书。
我想任何有双向 SSL 连接和/或 VISA API 经验的人都能够理解这个错误背后的确切原因。
编辑:我能够从配置相同.p12
证书和授权标头的 SOAP UI 成功调用 API。