1. 获取访问网站需要的证书

2. 从微软官网下载“资源工具箱”,其中包括证书管理工具。
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17657

安装完,Windows HTTP 服务证书配置工具(WinHttpCertCfg.exe)通常应该在C:\Program Files\Windows Resource Kits\Tools文件夹下。

3. 使用CMD命令行,运行WinHttpCertCfg.exe工具;
WinHttpCertCfg.exe –g –c LOCAL_MACHINE\MY –i “你的证书地址” –a “授权的用户组/名” –p 密码


给IIS服务用户组授权并导入证书。


这样,就可以在IIS管理,证书页面可以看到刚才导入的证书。
重启IIS服务,使刚才的授权生效。

4. 导出证书为.CER文件
开始—》运行—》输入MMC;打开控制台
文件—》添加/删除管理单元
选择“证书”,点击“添加”,选择“计算机账号”,“下一步”,“完成”,“确定”
选择“个人”—》“证书”;


右键单击证书,“所有任务”—》“导出”—》“下一步”—》“下一步”
选择

,点击“下一步”,单击“浏览”,输入要保存的文件名和地址,“下一步”—》“完成”;
导出成功!

5. C#使用证书进行SSL安全通信代码示例

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;//引用命名空间usingSystem.IO;usingSystem.Net;usingSystem.Net.Security;usingSystem.Security.Authentication;usingSystem.Security.Cryptography.X509Certificates;publicpartialclassssl:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){//验证服务器证书回调方法ServicePointManager.ServerCertificateValidationCallback=newSystem.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);//创建HttpWebRequest对象HttpWebRequesthttps=(HttpWebRequest)HttpWebRequest.Create("https://localhost/restful/test/BBBAAA");//创建证书X509Certificateobj509=newX509Certificate(AppDomain.CurrentDomain.BaseDirectory+"cert\\ccc.cer");//写入正确的证书路径(第四步导出的Cer文件)//添加证书到HTTP请求中https.ClientCertificates.Add(obj509);https.Method="GET";//获取请求返回的数据HttpWebResponseresponse=(HttpWebResponse)https.GetResponse();//读取返回的信息StreamReadersr=newStreamReader(response.GetResponseStream(),true);intcount;char[]ReadBuf=newchar[1024];do{count=sr.Read(ReadBuf,0,1024);if(0!=count){Label3.Text=newstring(ReadBuf);}}while(count>0);}//重写证书验证方法,总是返回TRUE,解决未能为SSL/TLS安全通道建立信任关系的问题publicboolCheckValidationResult(objectsender,X509Certificatecertificate,X509Chainchain,SslPolicyErrorserrors){//总是返回TRUEreturntrue;}}