作法:使用傳輸安全性和訊息認證

同時使用傳輸與訊息認證來保護服務的安全時,需要在 Windows Communication Foundation (WCF) 中同時運用最佳的傳輸與訊息安全性模式。 簡單地說,傳輸層安全性可提供完整性與機密性,而訊息層安全性則提供各種在嚴格的傳輸安全性機制中不可能提供的認證。 本主題將說明使用 WSHttpBindingNetTcpBinding 繫結,以訊息認證來實作傳輸時的基本步驟。 如需設定安全性模式的詳細資訊,請參閱如何:設定安全性模式

當您將安全性模式設為 TransportWithMessageCredential 時,傳輸會決定用來提供傳輸層安全性的實際機制。 對 HTTP 來說,此機制為 Secure Sockets Layer (SSL) over HTTP (HTTPS);對 TCP 來說,此機制是 SSL over TCP 或 Windows 安全性。

如果傳輸機制為 HTTP (使用 WSHttpBinding),則 SSL over HTTP 會提供傳輸層級的安全性。 在此情況下,您必須設定電腦使用繫結至連接埠的 SSL 憑證來裝載服務 (如本主題稍後所示)。

如果傳輸機制為 TCP (使用 NetTcpBinding),則預設會提供的傳輸層安全性便是 Windows 安全性或 SSL over TCP。 一旦使用 SSL over TCP,您必須使用 SetCertificate 方法來指定憑證 (如本主題稍後所示)。

若要使用 WSHttpBinding 搭配憑證來獲得傳輸安全性 (透過程式碼)

  1. 請使用 HttpCfg.exe 工具,將 SSL 憑證繫結至電腦的連接埠。 如需詳細資訊,請參閱如何:使用 SSL 憑證設定連接埠

  2. 建立 WSHttpBinding 類別的執行個體,並將 Mode 屬性設定為 TransportWithMessageCredential

  3. ClientCredentialType 屬性設定為適當值。 (如需詳細資訊,請參閱選取認證類型 (部分機器翻譯)。)下列程式碼會使用 Certificate 值。

  4. 使用適當的基底位址,建立 Uri 類別的執行個體 請注意,位址必須使用 "HTTPS" 配置,而且必須包含電腦的實際名稱,以及 SSL 憑證所繫結的連接埠號碼。 (另外,您也可以在組態中設定基底位址)。

  5. 使用 AddServiceEndpoint 方法新增服務端點。

  6. 如下列程式碼所示,建立 ServiceHost 的執行個體並呼叫 Open 方法。

    WSHttpBinding b = new WSHttpBinding();
    b.Security.Mode = SecurityMode.TransportWithMessageCredential;
    b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
    
    // The SSL certificate is bound to port 8006 using the HttpCfg.exe tool.
    Uri httpsAddress = new Uri("https://localMachineName:8006/base");
    ServiceHost sh = new ServiceHost(typeof(Calculator), httpsAddress);
    sh.AddServiceEndpoint(typeof(ICalculator), b, "HttpsCalculator");
    sh.Open();
    Console.WriteLine("Listening");
    Console.ReadLine();
    
    Dim b As New WSHttpBinding()
    b.Security.Mode = SecurityMode.TransportWithMessageCredential
    b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate
    
    ' The SSL certificate is bound to port 8006 using the HttpCfg.exe tool.
    Dim httpsAddress As New Uri("https://localMachineName:8006/base")
    Dim sh As New ServiceHost(GetType(Calculator), httpsAddress)
    sh.AddServiceEndpoint(GetType(ICalculator), b, "HttpsCalculator")
    sh.Open()
    Console.WriteLine("Listening")
    Console.ReadLine()
    

若要使用 NetTcpBinding 搭配憑證來獲得傳輸安全性 (透過程式碼)

  1. 建立 NetTcpBinding 類別的執行個體,並將 Mode 屬性設定為 TransportWithMessageCredential

  2. ClientCredentialType 設定為適當值。 下列程式碼會使用 Certificate 值。

  3. 使用適當的基底位址,建立 Uri 類別的執行個體 請注意,位址必須使用 "net.tcp" 配置。 (另外,您也可以在組態中設定基底位址)。

  4. 建立 ServiceHost 類別的執行個體。

  5. 使用 SetCertificate 類別的 X509CertificateRecipientServiceCredential 方法,明確設定服務的 X.509 憑證。

  6. 使用 AddServiceEndpoint 方法新增服務端點。

  7. 呼叫 Open 方法,如下列程式碼所示。

    NetTcpBinding b = new NetTcpBinding(SecurityMode.TransportWithMessageCredential);
    b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
    Uri netTcpAddress = new Uri("net.tcp://baseAddress");
    ServiceHost sh = new ServiceHost(typeof(Calculator), netTcpAddress);
    sh.Credentials.ServiceCertificate.SetCertificate(
        StoreLocation.LocalMachine, StoreName.My,
        X509FindType.FindByIssuerName, "Contoso.com");
    sh.AddServiceEndpoint(typeof(ICalculator), b, "TcpCalculator");
    sh.Open();
    Console.WriteLine("Listening");
    Console.ReadLine();
    
    Dim b As New NetTcpBinding(SecurityMode.TransportWithMessageCredential)
    b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate
    Dim netTcpAddress As New Uri("net.tcp://baseAddress")
    Dim sh As New ServiceHost(GetType(Calculator), netTcpAddress)
    sh.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByIssuerName, "Contoso.com")
    sh.AddServiceEndpoint(GetType(ICalculator), b, "TcpCalculator")
    sh.Open()
    Console.WriteLine("Listening")
    Console.ReadLine()
    

若要使用 NetTcpBinding 搭配 Windows 來獲得傳輸安全性 (透過程式碼)

  1. 建立 NetTcpBinding 類別的執行個體,並將 Mode 屬性設定為 TransportWithMessageCredential

  2. ClientCredentialType 設定為 Windows,藉此將傳輸安全性設定為使用 Windows (請注意,此設定為預設值)。

  3. ClientCredentialType 設定為適當值。 下列程式碼會使用 Certificate 值。

  4. 使用適當的基底位址,建立 Uri 類別的執行個體 請注意,位址必須使用 "net.tcp" 配置。 (另外,您也可以在組態中設定基底位址)。

  5. 建立 ServiceHost 類別的執行個體。

  6. 使用 SetCertificate 類別的 X509CertificateRecipientServiceCredential 方法,明確設定服務的 X.509 憑證。

  7. 使用 AddServiceEndpoint 方法新增服務端點。

  8. 呼叫 Open 方法,如下列程式碼所示。

    NetTcpBinding b = new NetTcpBinding(SecurityMode.TransportWithMessageCredential);
    b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
    b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
    Uri netTcpAddress = new Uri("net.tcp://Tcp");
    ServiceHost sh = new ServiceHost(typeof(Calculator), netTcpAddress);
    sh.Credentials.ServiceCertificate.SetCertificate(
        StoreLocation.LocalMachine, StoreName.My,
        X509FindType.FindByIssuerName, "Contoso.com");
    sh.AddServiceEndpoint(typeof(ICalculator), b, "TcpCalculator");
    sh.Open();
    Console.WriteLine("Listening");
    Console.ReadLine();
    
    Dim b As New NetTcpBinding(SecurityMode.TransportWithMessageCredential)
    b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows
    b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate
    Dim netTcpAddress As New Uri("net.tcp://Tcp")
    Dim sh As New ServiceHost(GetType(Calculator), netTcpAddress)
    sh.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByIssuerName, "Contoso.com")
    sh.AddServiceEndpoint(GetType(ICalculator), b, "TcpCalculator")
    sh.Open()
    Console.WriteLine("Listening")
    Console.ReadLine()
    

使用組態

若要使用 WSHttpBinding

  1. 使用繫結至連接埠的 SSL 憑證來設定電腦 (如需詳細資訊,請參閱如何:使用 SSL 憑證設定連接埠)。 您不需要使用此組態來設定 <transport> 元素值。

  2. 指定訊息層級安全性的用戶端認證類型。 下列範例會將 <message> 元素的 clientCredentialType 屬性設定為 UserName

    <wsHttpBinding>  
    <binding name="WsHttpBinding_ICalculator">  
            <security mode="TransportWithMessageCredential" >  
               <message clientCredentialType="UserName" />  
            </security>  
    </binding>  
    </wsHttpBinding>  
    

若要使用 NetTcpBinding 搭配憑證來獲得傳輸安全性

  1. 如果要使用 SSL over TCP,您必須在 <behaviors> 項目中明確指定憑證。 下列範例會在預設的存放區位置中 (本機電腦與個人存放區),按照憑證簽發者指定憑證。

    <behaviors>  
     <serviceBehaviors>  
       <behavior name="mySvcBehavior">  
           <serviceCredentials>  
             <serviceCertificate findValue="contoso.com"  
                                 x509FindType="FindByIssuerName" />  
           </serviceCredentials>  
       </behavior>  
     </serviceBehaviors>  
    </behaviors>  
    
  2. <netTcpBinding> 新增至繫結區段

  3. 新增繫結項目,然後將 name 屬性設定為適當值。

  4. 加入 <security> 元素,並將 mode 屬性設定為 TransportWithMessageCredential

  5. 加入 <message> 元素,並將 clientCredentialType 屬性設定為適當值。

    <bindings>  
    <netTcpBinding>  
      <binding name="myTcpBinding">  
        <security mode="TransportWithMessageCredential" >  
           <message clientCredentialType="Windows" />  
        </security>  
      </binding>  
    </netTcpBinding>  
    </bindings>  
    

若要使用 NetTcpBinding 搭配 Windows 來獲得傳輸安全性

  1. <netTcpBinding> 新增至繫結區段,

  2. 加入 <binding> 項目,並將 name 屬性設定為適當值。

  3. 加入 <security> 元素,並將 mode 屬性設定為 TransportWithMessageCredential

  4. 加入 <transport> 元素,並將 clientCredentialType 屬性設定為 Windows

  5. 加入 <message> 項目,並將 clientCredentialType 屬性設定為適當值。 下列程式碼會設定憑證的值。

    <bindings>  
    <netTcpBinding>  
      <binding name="myTcpBinding">  
        <security mode="TransportWithMessageCredential" >  
           <transport clientCredentialType="Windows" />  
           <message clientCredentialType="Certificate" />  
        </security>  
      </binding>  
    </netTcpBinding>  
    </bindings>  
    

另請參閱