如何使用 SSL 證書配置端口


 

創建使用自承載的 Windows Communication Foundation (WCF) 服務時WSHttpBinding類,使用傳輸安全,還必須使用 X.509 證書配置端口。 如果不是在創建自承載服務,可以在 Internet 信息服務 (IIS) 上承載服務。 有關詳細信息,請參閱HTTP 傳輸安全性

 

若要配置端口,使用的工具取決於計算機運行的操作系統。

如果運行的是 Windows Server 2003 或 Windows XP,則使用 HttpCfg.exe 工具。 Windows Server 2003 中已安裝該工具。 Windows XP,可以下載該工具在Windows XP Service Pack 2 支持工具。 有關詳細信息,請參閱Httpcfg 概述。 Windows 支持工具文檔說明了 Httpcfg.exe 工具的語法。

如果運行的是 Windows Vista,則使用已安裝的 Netsh.exe 工具。

本主題介紹如何完成以下一些過程:

  • 確定計算機當前的端口配置。

  • 獲取證書的指紋(以下兩個過程需要證書指紋)。

  • 將 SSL 證書綁定到端口配置。

  • 將 SSL 證書綁定到端口配置並支持客戶端證書。

  • 從某個端口號刪除 SSL 證書。

請注意,修改存儲於計算機上的證書需要管理特權。

確定如何配置端口

  1. 在中Windows Server 2003或Windows XP,使用 HttpCfg.exe 工具查看當前端口配置中,使用查詢並ssl切換時,如下面的示例中所示。

    httpcfg query ssl  
    
  2. 在 Windows Vista 中,使用 Netsh.exe 工具查看當前端口配置,如下面的示例所示。

    netsh http show sslcert  
    

獲取證書的指紋

  1. 使用證書 MMC 管理單元查找用於客戶端身份驗證的 X.509 證書。 有關詳細信息,請參閱如何:使用 MMC 管理單元查看證書

  2. 訪問證書的指紋。 有關詳細信息,請參閱如何: 檢索證書的指紋

  3. 將證書指紋復制到文本編輯器,如 Notepad。

  4. 移除十六進制字符之間的所有空格。 完成此操作的一種方法是使用文本編輯器的“查找和替換”功能,將每個空格替換為空字符。

將 SSL 證書綁定至端口號

  1. 在 Windows Server 2003 或 Windows XP 中,對安全套接字層 (SSL) 存儲區使用 HttpCfg.exe 工具的“set”命令將證書綁定至端口號。 該工具使用指紋識別證書,如下面的示例所示。

    httpcfg set ssl -i 0.0.0.0:8012 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6  
    
    • -I交換機具有的語法IP:port ,指示該工具將證書設置為計算機的端口 8012。 另外,也可將端口號前面的四個零替換為計算機的實際 IP 地址。

    • -H開關指定證書的指紋。

  2. 在 Windows Vista 中使用 Netsh.exe 工具,如下面的示例所示。

    netsh http add sslcert ipport=0.0.0.0:8000 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF}   
    
    • Certhash參數指定的證書的指紋。

    • Ipport參數指定的 IP 地址和端口,以及功能類似 -i前述 Httpcfg.exe 工具的開關。

    • Appid參數是一個 GUID,用於標識所屬應用程序。

將 SSL 證書綁定至端口號並支持客戶端證書

  1. 在 Windows Server 2003 或 Windows XP 中,若要支持在傳輸層使用 X.509 證書進行身份驗證的客戶端,請按照前面的步驟進行操作,但要向 HttpCfg.exe 另外傳遞一個命令行參數,如下面的示例所示。

    httpcfg set ssl -i 0.0.0.0:8012 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6 -f 2  
    

    -F開關的語法的n其中 n 是介於 1 和 7 之間的數字。 值為 2 可在傳輸層啟用客戶端證書,如上面的示例所示。 值為 3 可啟用客戶端證書並將這些證書映射至 Windows 帳戶。請參見“HttpCfg.exe 幫助”以獲取其他值的行為。

  2. 在 Windows Vista 中,若要支持在傳輸層使用 X.509 證書進行身份驗證的客戶端,請按照前面的步驟進行操作,但要另外提供一個參數,如下面的示例所示。

    netsh http add sslcert ipport=0.0.0.0:8000 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF} clientcertnegotiation=enable  
    

刪除端口號的 SSL 證書

  1. 使用 HttpCfg.exe 或 Netsh.exe 工具查看計算機上的端口和所有綁定的指紋。 若要將信息輸出到磁盤,請使用重定向字符“>”,如下面的示例所示。

    httpcfg query ssl>myMachinePorts.txt  
    
  2. 在中Windows Server 2003或Windows XP,使用 HttpCfg.exe 工具以及刪除並ssl關鍵字。使用 -i開關指定IP:port數,並且 -h開關指定指紋。

    httpcfg delete ssl -i 0.0.0.0:8005 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6  
    
  3. 在 Windows Vista 中使用 Netsh.exe 工具,如下面的示例所示。

    Netsh http delete sslcert ipport=0.0.0.0:8005  
    

示例

下面的代碼演示如何使用 WSHttpBinding 類(設置為傳輸安全)創建自承載服務。 創建應用程序時,請指定地址中的端口號。

C#
// This string uses a function to prepend the computer name at run time. string addressHttp = String.Format( "http://{0}:8080/Calculator", System.Net.Dns.GetHostEntry("").HostName); WSHttpBinding b = new WSHttpBinding(); b.Security.Mode = SecurityMode.Transport; b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate; // You must create an array of URI objects to have a base address. Uri a = new Uri(addressHttp); Uri[] baseAddresses = new Uri[] { a }; // Create the ServiceHost. The service type (Calculator) is not // shown here. ServiceHost sh = new ServiceHost(typeof(Calculator), baseAddresses); // Add an endpoint to the service. Insert the thumbprint of an X.509 // certificate found on your computer. Type c = typeof(ICalculator); sh.AddServiceEndpoint(c, b, "MyCalculator"); sh.Credentials.ServiceCertificate.SetCertificate( StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, "contoso.com"); // This next line is optional. It specifies that the client's certificate // does not have to be issued by a trusted authority, but can be issued // by a peer if it is in the Trusted People store. Do not use this setting // for production code. The default is PeerTrust, which specifies that // the certificate must originate from a trusted certifiate authority. // sh.Credentials.ClientCertificate.Authentication.CertificateValidationMode = // X509CertificateValidationMode.PeerOrChainTrust; try { sh.Open(); string address = sh.Description.Endpoints[0].ListenUri.AbsoluteUri; Console.WriteLine("Listening @ {0}", address); Console.WriteLine("Press enter to close the service"); Console.ReadLine(); sh.Close(); } catch (CommunicationException ce) { Console.WriteLine("A commmunication error occurred: {0}", ce.Message); Console.WriteLine(); } catch (System.Exception exc) { Console.WriteLine("An unforseen error occurred: {0}", exc.Message); Console.ReadLine(); }

原文 https://docs.microsoft.com/zh-cn/dotnet/framework/wcf/feature-details/how-to-configure-a-port-with-an-ssl-certificate

請參閱

反饋

我們衷心期待你的反饋意見。 請選擇要提供的反饋類型:

 

我們就 GitHub 問題建立了新的反饋系統。 閱讀我們的博客文章,了解相關內容。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM