溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ASP.NET(C#)使用證書(shū)進(jìn)行SSL安全通信

發(fā)布時(shí)間:2020-06-14 05:19:22 來(lái)源:網(wǎng)絡(luò) 閱讀:4354 作者:倚己修心 欄目:編程語(yǔ)言

 1.      獲取訪問(wèn)網(wǎng)站需要的證書(shū)

2.      從微軟官網(wǎng)下載“資源工具箱”,其中包括證書(shū)管理工具。
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17657

安裝完,Windows HTTP 服務(wù)證書(shū)配置工具(WinHttpCertCfg.exe)通常應(yīng)該在C:\Program Files\Windows Resource Kits\Tools文件夾下。

3.      使用CMD命令行,運(yùn)行WinHttpCertCfg.exe工具;
WinHttpCertCfg.exe –g –c LOCAL_MACHINE\MY –i “
你的證書(shū)地址” –a “授權(quán)的用戶組/” –p 密碼

ASP.NET(C#)使用證書(shū)進(jìn)行SSL安全通信


IIS服務(wù)用戶組授權(quán)并導(dǎo)入證書(shū)。

 

ASP.NET(C#)使用證書(shū)進(jìn)行SSL安全通信


這樣,就可以在IIS管理,證書(shū)頁(yè)面可以看到剛才導(dǎo)入的證書(shū)。
重啟IIS服務(wù),使剛才的授權(quán)生效。

4.      導(dǎo)出證書(shū)為.CER文件
開(kāi)始—》運(yùn)行—》輸入MMC;打開(kāi)控制臺(tái)
文件—》添加/刪除管理單元
選擇“證書(shū)”,點(diǎn)擊“添加”,選擇“計(jì)算機(jī)賬號(hào)”,“下一步”,“完成”,“確定”
選擇“個(gè)人”—》“證書(shū)”;

 

ASP.NET(C#)使用證書(shū)進(jìn)行SSL安全通信


右鍵單擊證書(shū),“所有任務(wù)”—》“導(dǎo)出”—》“下一步”—》“下一步”
選擇

ASP.NET(C#)使用證書(shū)進(jìn)行SSL安全通信

,點(diǎn)擊“下一步”,單擊“瀏覽”,輸入要保存的文件名和地址,“下一步”—》“完成”;
導(dǎo)出成功!

5.   C#使用證書(shū)進(jìn)行SSL安全通信代碼示例

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Web; 
  5. using System.Web.UI; 
  6. using System.Web.UI.WebControls; 
  7. //引用命名空間 
  8. using System.IO; 
  9. using System.Net; 
  10. using System.Net.Security; 
  11. using System.Security.Authentication; 
  12. using System.Security.Cryptography.X509Certificates; 
  13.  
  14. public partial class ssl : System.Web.UI.Page 
  15.     protected void Page_Load(object sender, EventArgs e) 
  16.     { 
  17.         //驗(yàn)證服務(wù)器證書(shū)回調(diào)方法 
  18.         ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); 
  19.         //創(chuàng)建HttpWebRequest對(duì)象 
  20.         HttpWebRequest https = (HttpWebRequest)HttpWebRequest.Create("https://localhost/restful/test/BBBAAA"); 
  21.         //創(chuàng)建證書(shū) 
  22.         X509Certificate obj509 = new X509Certificate(AppDomain.CurrentDomain.BaseDirectory+"cert\\ccc.cer");//寫(xiě)入正確的證書(shū)路徑(第四步導(dǎo)出的Cer文件) 
  23.         //添加證書(shū)到HTTP請(qǐng)求中 
  24.         https.ClientCertificates.Add(obj509); 
  25.         https.Method = "GET"
  26.         //獲取請(qǐng)求返回的數(shù)據(jù) 
  27.         HttpWebResponse response = (HttpWebResponse)https.GetResponse(); 
  28.         //讀取返回的信息 
  29.         StreamReader sr = new StreamReader(response.GetResponseStream(), true); 
  30.         int count; 
  31.         char[] ReadBuf = new char[1024]; 
  32.         do 
  33.         { 
  34.             count = sr.Read(ReadBuf, 0, 1024); 
  35.             if (0 != count) 
  36.             { 
  37.                 Label3.Text = new string(ReadBuf); 
  38.             } 
  39.         } while (count > 0); 
  40.     } 
  41.     //重寫(xiě)證書(shū)驗(yàn)證方法,總是返回TRUE,解決未能為SSL/TLS安全通道建立信任關(guān)系的問(wèn)題 
  42.     public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) 
  43.     { 
  44.         //總是返回TRUE 
  45.         return true
  46.     } 


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI