• .net core 调用数字证书 使用X509Certificate2


     .NET下面的 .netfromwork使用和asp.net core下使用方式不一样

    配置文件中代码:

            public const string API_URL = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
    ///
    <summary> /// 本地或者服务器的证书位置 /// </summary> public const string CertPath = @"D:apiclient_cert.p12"; /// <summary> /// 本地或者服务器的证书密码 /// </summary> public const string CApassword = "9999999";
    Build_Content()
      这个主要是传递的参数封装值
     public async Task<bool> Send_Transfer()
            {
                //HttpClient请求,在handler里添加X509Certificate2 证书,数据data是byte[] 类型,所以需要使用ByteArrayContent传入
                var handler = new HttpClientHandler();
                handler.ClientCertificateOptions = ClientCertificateOption.Manual;
                handler.SslProtocols = SslProtocols.Tls12;
                //获取证书路径
                //商户私钥证书,用于对请求报文进行签名
                try
                {
                    handler.ClientCertificates.Add(new X509Certificate2(WeixinPay.CertPath, WeixinPay.CApassword, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet));
                }
                catch (Exception e)
                {
                    _logger.LogError(e.Message);
                }
                handler.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;
                handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
                //post请求
                var client = new HttpClient(handler);
                /////////////////////////////////////////////////////////// 
                //以下属于在.netfromwork环境下写法
               // ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                //调用证书
                //X509Certificate2 cer = new X509Certificate2(WeixinPay.CertPath, WeixinPay.CApassword, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);
                //handler.ClientCertificates.Add(cer);
                ///////////////////////////////////////////////////////////
                response = await client.PostAsync(WeixinPay.API_URL, Build_Content());
                ///////////////////////////////////////////////////////////
                return response.IsSuccessStatusCode;
            }
  • 相关阅读:
    linux启动流程
    控制nginx并发链接数量和客户端请求nginx的速率
    MySQL修改密码
    nginx站点目录及文件URL访问控制
    nginx日志相关优化安全
    根据参数优化nginx的服务性能
    nginx基本安全优化
    nginx rewrite
    nginx location
    nginx访问日志(access_log)
  • 原文地址:https://www.cnblogs.com/Warmsunshine/p/8250507.html
Copyright © 2020-2023  润新知