1

如果它是重复的,我很抱歉。我在网上看到几篇关于这个话题的帖子。但是没有得到任何适当的解决方案。我在 nopcommerce 4.0 工作,它在 .net 核心上运行,但目标框架是 4.xx 我正在开发一个支付插件,它需要与 .crt 和 .key 文件进行安全连接。付款方式提供商发送一个按预期工作的 php 文件。下面是示例代码

function ProcessRequest($curl_post_data,$service_url,$proxy,$proxyauth)
{
$output = '';
$certfile       = '/createorder.crt';
$keyfile        = '/createorder.key';
$cert_password = '';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $service_url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); 
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_SSLCERT, getcwd() . $certfile );
curl_setopt( $ch, CURLOPT_SSLKEY, getcwd() . $keyfile );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$output = curl_exec($ch);
if (curl_error($ch)) {
   echo $error_msg = curl_error($ch);
}
$cblcz = json_decode($output, true );
return $cblcz;
}

$proxy ="";
$proxyauth ="";
$postDatatoken = '{
"password": "123456Aa",
"userName": "test"
}';
$serviceUrltoken ="";
$serviceUrltoken= 'https://sandbox.thecitybank.com:7788/transaction/token';
$cblcz = ProcessRequest($postDatatoken,$serviceUrltoken,$proxy,$proxyauth);

我无法将此 curl 转换为 http 帖子。我试过下面的链接

C# 和其他中的 PHP/Curl-SSL 操作替代方案,但没有解决任何问题。

不工作。对于openssl,它抛出的依赖没有加载。这是我的 C# 代码

try
            {
                ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                       | SecurityProtocolType.Tls11
                       | SecurityProtocolType.Tls12
                       | SecurityProtocolType.Ssl3;

                var certPath = Path.Combine(CommonHelper.MapPath("~/Plugins/Payments.CityBankApi/"), "createorder.crt");
                var keyPath = Path.Combine(CommonHelper.MapPath("~/Plugins/Payments.CityBankApi/"), "createorder.key");


               string certificateText = File.ReadAllText(certPath);
                string privateKeyText = File.ReadAllText(keyPath);

                ICertificateProvider provider = new CertificateFromFileProvider(certificateText, privateKeyText);
                //X509Certificate certificate = provider.Certificate;

                string accessTokenUrl = "https://sandbox.thecitybank.com:7788/transaction/token";

                var requestUrl = new Uri(accessTokenUrl);
                var request = (HttpWebRequest)WebRequest.Create(accessTokenUrl);
                request.ContentType = "application/json";
                request.Method = "POST";

                request.ClientCertificates.Add(provider.Certificate);


                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    string json = "{\"userName\":\"test\"," +
                                  "\"password\":\"123456Aa\"}";

                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
                var httpResponse = (HttpWebResponse)request.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                }
            }

            catch (Exception ex)
            {
                throw ex;
            }
4

1 回答 1

0

您的代码似乎正确。但请确保您有有效的证书。如果认证不正确,它将通过异常处理。检查您的异常消息并尝试检测确切的错误。或在此处发布您的错误消息。

此外,您应该添加

request.ServerCertificateValidationCallback = (e,r,c,n) => true;
于 2019-03-06T13:19:20.837 回答