一、命名空间
using System.Security.Cryptography.X509Certificates;
二、调用代码
string certPath = Server.MapPath("/weixinApp/cert/apiclient_cert.p12"); //证书已上传到对应目录 string password = "1244531402"; //证书密码 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); X509Certificate cert = new X509Certificate(certPath, password);
/// <summary> /// CheckValidationResult的定义 /// </summary> /// <param name="sender"></param> /// <param name="certificate"></param> /// <param name="chain"></param> /// <param name="errors"></param> /// <returns></returns> private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { if (errors == SslPolicyErrors.None) return true; return false; }