如何使用 SSL 憑證設定埠
在建立使用傳輸安全性的類別 WSHttpBinding 的自我裝載 Windows Communication Foundation(WCF)服務時,您也必須使用 X.509 憑證來設定埠。 如果您未建立自我裝載服務,您可以在 Internet Information Services (IIS) 上裝載服務。 如需詳細資訊,請參閱 HTTP 傳輸安全性。
若要設定埠,您使用的工具取決於您計算機上執行的作系統。
如果您正在執行 Windows Server 2003,請使用 HttpCfg.exe 工具。 在 Windows Server 2003 上,已安裝此工具。 如需詳細資訊,請參閱 Httpcfg 概觀。 Windows 支援工具文件說明 Httpcfg.exe 工具的語法。
如果您正在執行 Windows Vista,請使用已安裝的 Netsh.exe 工具。
備註
修改儲存在電腦上的憑證需要系統管理許可權。
確定埠的配置方式
在 Windows Server 2003 或 Windows XP 中,使用 HttpCfg.exe 工具來檢視目前的埠組態,並使用 查詢 和 ssl 參數,如下列範例所示。
httpcfg query ssl
在 Windows Vista 中,使用 Netsh.exe 工具來檢視目前的埠組態,如下列範例所示。
netsh http show sslcert
取得憑證的指紋辨識碼
使用憑證 MMC 嵌入式管理單元來尋找具有客戶端驗證用途的 X.509 憑證。 如需詳細資訊,請參閱 如何:使用 MMC 嵌入式管理單元檢視憑證。
存取憑證的指紋。 如需詳細資訊,請參閱做法:擷取憑證的指紋。
將憑證的指紋複製到文本編輯器中,例如記事本。
拿掉十六進位字元之間的所有空格。 若要達成此目的,其中一個方法是使用文本編輯器的 find-and-replace 功能,並以 Null 字元取代每個空格。
將 SSL 憑證系結至埠號碼
在 Windows Server 2003 或 Windows XP 中,使用安全套接字層 (SSL) 存放區上「設定」模式中的 HttpCfg.exe 工具,將憑證系結至埠號碼。 此工具會使用指紋來識別憑證,如下列範例所示。
httpcfg set ssl -i 0.0.0.0:8012 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6
-i 參數的語法是
IP
:port
,並指示工具將憑證設定為電腦的埠 8012。 或者,數位前面的四個零也可以由計算機的實際IP位址取代。-h 選項會指定憑證的指紋。
在 Windows Vista 中,使用 Netsh.exe 工具,如下列範例所示。
netsh http add sslcert ipport=0.0.0.0:8000 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00001111-aaaa-2222-bbbb-3333cccc4444}
certhash 參數會指定憑證的指紋。
ipport 參數會指定IP位址和埠,而且函式就像所述的 Httpcfg.exe 工具的 -i 參數一樣。
appid 參數是 GUID,可用來識別擁有的應用程式。
將 SSL 憑證系結至埠號碼並支援客戶端憑證
在 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 說明。在 Windows Vista 中,若要支援在傳輸層使用 X.509 憑證進行驗證的用戶端,請遵循上述程式,但具有額外的參數,如下列範例所示。
netsh http add sslcert ipport=0.0.0.0:8000 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00001111-aaaa-2222-bbbb-3333cccc4444} clientcertnegotiation=enable
從埠號碼刪除 SSL 憑證
使用 HttpCfg.exe 或 Netsh.exe 工具來查看電腦上所有系結的埠和指紋。 若要將資訊列印到磁碟,請使用重新導向字元 “>”,如下列範例所示。
httpcfg query ssl>myMachinePorts.txt
在 Windows Server 2003 或 Windows XP 中,使用 HttpCfg.exe 工具搭配 delete 和 ssl 關鍵詞。 使用 -i 參數來指定
IP
:port
number,而 -h 參數則指定指紋。httpcfg delete ssl -i 0.0.0.0:8005 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6
在 Windows Vista 中,使用 Netsh.exe 工具,如下列範例所示。
Netsh http delete sslcert ipport=0.0.0.0:8005
範例
以下程式碼顯示如何使用設為傳輸安全性的WSHttpBinding類別來建立自我託管服務。 建立應用程式時,請在位址中指定埠號碼。
// 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 certificate authority.
// sh.Credentials.ClientCertificate.Authentication.CertificateValidationMode =
// X509CertificateValidationMode.PeerOrChainTrust;
try
{
sh.Open();
string address = sh.Description.Endpoints[0].ListenUri.AbsoluteUri;
Console.WriteLine($"Listening @ {address}");
Console.WriteLine("Press enter to close the service");
Console.ReadLine();
sh.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine($"A communication error occurred: {ce.Message}");
Console.WriteLine();
}
catch (System.Exception exc)
{
Console.WriteLine($"An unforeseen error occurred: {exc.Message}");
Console.ReadLine();
}
' This string uses a function to prepend the computer name at run time.
Dim addressHttp As String = String.Format("http://{0}:8080/Calculator", _
System.Net.Dns.GetHostEntry("").HostName)
Dim b As 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.
Dim a As New Uri(addressHttp)
Dim baseAddresses() As Uri = {a}
' Create the ServiceHost. The service type (Calculator) is not
' shown here.
Dim sh As New ServiceHost(GetType(Calculator), baseAddresses)
' Add an endpoint to the service. Insert the thumbprint of an X.509
' certificate found on your computer.
Dim c As Type = GetType(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 certificate authority.
' sh.Credentials.ClientCertificate.Authentication.CertificateValidationMode =
' X509CertificateValidationMode.PeerOrChainTrust
Try
sh.Open()
Dim address As String = sh.Description.Endpoints(0).ListenUri.AbsoluteUri
Console.WriteLine("Listening @ {0}", address)
Console.WriteLine("Press enter to close the service")
Console.ReadLine()
sh.Close()
Catch ce As CommunicationException
Console.WriteLine("A communication error occurred: {0}", ce.Message)
Console.WriteLine()
Catch exc As System.Exception
Console.WriteLine("An unforeseen error occurred: {0}", exc.Message)
Console.ReadLine()
End Try