我已经在我的 asp.net 核心API中添加了一个万事达卡sdk。对 api 的调用在我的本地计算机上工作正常。但是当部署在 azure 上时,它会引发上述错误。
我已经尝试了一切。
我已将调试器附加到活动站点。当我调用时会抛出错误设置身份验证
public MatchType RequestCall(string consumerKey, string keyAlias, string keyPassword)
{
byte[] certificateBytes = GetCertificateStream();
ApiConfig.SetAuthentication(new OAuthAuthentication(consumerKey, certificateBytes, keyAlias, keyPassword));
ApiConfig.SetSandbox(true);
RequestMap map = GenerateRequestMap();
TerminationInquiryRequest response = TerminationInquiryRequest.Create(map);
return GetMatchType(response);
}
public byte[] GetCertificateStream()
{
var resourceName = typeof(TerminationMatchInquiry).GetTypeInfo().Assembly.GetManifestResourceNames()[0];//Get certificate resource name
using (Stream CertStream = typeof(TerminationMatchInquiry).GetTypeInfo().Assembly.GetManifestResourceStream(resourceName))
{
byte[] RawBytes = new byte[CertStream.Length];
for (int Index = 0; Index < CertStream.Length; Index++)
{
RawBytes[Index] = (byte)CertStream.ReadByte();
}
return RawBytes.ToArray();
}
}
在查看了 MasterCard SDK for C# 的源代码后,我发现,当我们调用 OAuthAuthentication 时,它会创建一个 X509Certificate2 的实例。错误是在此行中引起的。
cert = new X509Certificate2(rawCertificateData, password, keyStoreFlags | X509KeyStorageFlags.Exportable);
在 Azure Web 应用中,如果我们尝试使用证书,则需要从 Azure 门户上传证书。在 Azure Web 应用应用程序中添加带有指纹值的WEBSITE_LOAD_CERTIFICATES。更多详细信息请参阅博客。