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安全通信代碼示例
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- //引用命名空間
- using System.IO;
- using System.Net;
- using System.Net.Security;
- using System.Security.Authentication;
- using System.Security.Cryptography.X509Certificates;
- public partial class ssl : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- //驗證服務器證書回調方法
- ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
- //創建HttpWebRequest對象
- HttpWebRequest https = (HttpWebRequest)HttpWebRequest.Create("https://localhost/restful/test/BBBAAA");
- //創建證書
- X509Certificate obj509 = new X509Certificate(AppDomain.CurrentDomain.BaseDirectory+"cert\\ccc.cer");//寫入正確的證書路徑(第四步導出的Cer文件)
- //添加證書到HTTP請求中
- https.ClientCertificates.Add(obj509);
- https.Method = "GET";
- //獲取請求返回的數據
- HttpWebResponse response = (HttpWebResponse)https.GetResponse();
- //讀取返回的信息
- StreamReader sr = new StreamReader(response.GetResponseStream(), true);
- int count;
- char[] ReadBuf = new char[1024];
- do
- {
- count = sr.Read(ReadBuf, 0, 1024);
- if (0 != count)
- {
- Label3.Text = new string(ReadBuf);
- }
- } while (count > 0);
- }
- //重寫證書驗證方法,總是返回TRUE,解決未能為SSL/TLS安全通道建立信任關系的問題
- public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
- {
- //總是返回TRUE
- return true;
- }
- }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。