作法:使用 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 工具。

注意

您必須具備系統管理權限,才能修改儲存在電腦上的憑證。

判斷如何設定連接埠

  1. 在 Windows Server 2003 或 Windows XP 中,使用 HttpCfg.exe 工具搭配 queryssl 參數來檢視目前的連接埠設定,如下列範例所示。

    httpcfg query ssl  
    
  2. 在 Windows Vista 中,使用 Netsh.exe 工具來檢視目前的連接埠設定,如下列範例所示。

    netsh http show sslcert  
    

取得憑證的指紋

  1. 使用憑證 MMC 嵌入式管理單元,尋找目的為用戶端驗證的 X.509 憑證。 如需詳細資訊,請參閱如何:使用 MMC 嵌入式管理單元來檢視憑證

  2. 存取憑證的指紋。 如需詳細資訊,請參閱如何:擷取憑證的指紋

  3. 將憑證的指紋複製到文字編輯器中,例如「記事本」。

  4. 移除十六進位字元之間的所有空格。 完成這項工作的方法之一是使用文字編輯器的尋找與取代功能,並使用 null 字元來取代各個空格。

將 SSL 憑證繫結至連接埠號碼

  1. 在 Windows Server 2003 或 Windows XP 中,在安全通訊端層 (SSL) 存放區上的 "set" 模式中使用 HttpCfg.exe 工具,將憑證繫結至連接埠號碼。 此工具是使用指紋來識別憑證,如下列範例所示。

    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 參數 (Parameter) 會指定 IP 位址和連接埠,作用就和上述 Httpcfg.exe 工具的 -i 參數 (Switch) 一樣。

    • 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 中,搭配 deletessl 關鍵字使用 HttpCfg.exe 工具。 使用 -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 類別來建立自我裝載的服務。 當建立應用程式時,請在位址中指定連接埠號碼。

// 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 @ {0}", address);
    Console.WriteLine("Press enter to close the service");
    Console.ReadLine();
    sh.Close();
}
catch (CommunicationException ce)
{
    Console.WriteLine("A communication error occurred: {0}", ce.Message);
    Console.WriteLine();
}
catch (System.Exception exc)
{
    Console.WriteLine("An unforeseen error occurred: {0}", 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

另請參閱