傳輸安全性概觀

Windows Communication Foundation (WCF) 中的傳輸安全性機制主要取決於所使用的繫結和傳輸。 例如,使用 WSHttpBinding 類別時,傳輸為 HTTP,而保護此傳輸的主要機制則為 Secure Sockets Layer (SSL) over HTTP,通常稱為 HTTPS。 本主題將討論 WCF 系統提供繫結中所使用的主要傳輸安全性機制。

注意

SSL 安全性與 .NET Framework 3.5 (含) 以後版本搭配使用時,WCF 用戶端會使用其憑證存放區中的中繼憑證以及在 SSL 交涉期間收到的中繼憑證,在服務的憑證上執行憑證鏈結驗證。 .NET Framework 3.0 只會使用安裝在本機憑證存放區中的中繼憑證。

警告

使用傳輸安全性時,可能會覆寫 Thread.CurrentPrincipal 屬性。 為了避免發生這種情況,請將 ServiceAuthorizationBehavior.PrincipalPermissionMode 設定為 PrincipalPermissionMode.NoneServiceAuthorizationBehavior 是可在服務描述上設定的服務行為。

BasicHttpBinding

根據預設,BasicHttpBinding 類別不會提供安全性。 這個繫結是設計用來與未實作安全性的 Web 服務提供者互通。 不過,您可以透過將 Mode 屬性設定為 None 以外的任何值來啟動安全性。 若要啟用傳輸安全性,請將此屬性設為 Transport

與 IIS 互通

BasicHttpBinding 類別主要用於與現有的 Web 服務互通,而這些服務當中有多數是由網際網路資訊服務 (IIS) 所裝載。 因此,這個繫結的傳輸安全性是設計成可用來與 IIS 站台進行流暢的互通。 將安全性模式設定為 Transport,並接著設定用戶端認證類型,便可進行流暢的互通。 認證類型值會對應至 IIS 目錄安全性機制。 下列程式碼會示範所設定的模式以及設定為 Windows 的認證類型。 您可以在用戶端和伺服器都位於相同的 Windows 網域時使用這個組態。

BasicHttpBinding b = new BasicHttpBinding();
b.Security.Mode = BasicHttpSecurityMode.Transport ;
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
Dim b As BasicHttpBinding = New BasicHttpBinding()
b.Security.Mode = BasicHttpSecurityMode.Transport
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows

或者,使用下列組態:

<bindings>  
  <basicHttpBinding>  
    <binding name="SecurityByTransport">  
      <security mode="Transport">  
        <transport clientCredentialType="Windows" />  
       </security>  
     </binding>  
  </basicHttpBinding>  
</bindings>  

下列小節將討論其他的用戶端認證類型。

基本

這種類型會對應至 IIS 中的基本驗證方法。 在使用此模式時,IIS 伺服器必須透過 Windows 使用者帳戶和適當的 NTFS 檔案系統權限進行設定。 如需 IIS 6.0 的詳細資訊,請參閱啟用基本驗證和設定領域名稱。 如需 IIS 7.0 的詳細資訊,請參閱設定基本驗證 (IIS 7)

憑證

IIS 有一個會要求用戶端使用憑證登入的選項。 該功能也能讓 IIS 將用戶端憑證對應至 Windows 帳戶。 如需 IIS 6.0 的詳細資訊,請參閱在 IIS 6.0 中啟用用戶端憑證。 如需 IIS 7.0 的詳細資訊,請參閱在 IIS 7 中設定伺服器憑證

Digest

摘要式驗證類似於基本驗證,但是它具備以雜湊而非純文字形式來傳送認證的優點。 如需 IIS 6.0 的詳細資訊,請參閱 IIS 6.0 中的摘要式驗證。 如需 IIS 7.0 的詳細資訊,請參閱設定摘要驗證 (IIS 7)

Windows

這個類型會對應至 IIS 中的整合式 Windows 驗證。 當設定為這個值時,該伺服器必須出現在以 Kerberos 通訊協定做為網域控制站的 Windows 網域上。 如果伺服器未存在以 Kerberos 為基礎的網域中,或是如果 Kerberos 系統失敗,您可以使用下一節所介紹的 NT LAN Manager (NTLM) 值。 如需 IIS 6.0 的詳細資訊,請參閱 IIS 6.0 中的整合式 Windows 驗證。 如需 IIS 7.0 的詳細資訊,請參閱在 IIS 7 中設定伺服器憑證

NTLM

這個類型可讓伺服器在 Kerberos 通訊協定失敗時,使用 NTLM 進行驗證。 如需在 IIS 6.0 中設定 IIS 的詳細資訊,請參閱強制執行 NTLM 驗證。 若是 IIS 7.0,Windows 驗證會包含 NTLM 驗證。 如需詳細資訊,請參閱 在 IIS 7 中設定伺服器憑證

WsHttpBinding

WSHttpBinding 類別主要是用來與實作 WS-* 規格的服務進行交互操作。 此繫結的傳輸安全性為使用 HTTP 或 HTTPS 的安全通訊端層 (SSL)。 若要建立使用 SSL 的 WCF 應用程式,請使用 IIS 裝載應用程式。 或者,如果您要建立自我裝載的應用程式,請使用 HttpCfg.exe 工具將 X.509 憑證繫結至電腦上的特定連接埠。 連接埠號碼會指定做為 WCF 應用程式中的端點位址。 當使用傳輸模式時,端點位址必須包含 HTTPS 通訊協定,否則會在執行階段擲回例外狀況。 如需詳細資訊,請參閱 HTTP 傳輸安全性

若是用戶端驗證,請將 ClientCredentialType 類別的 HttpTransportSecurity 屬性設定為其中一個 HttpClientCredentialType 列舉值。 這些列舉值會與 BasicHttpBinding 的用戶端認證類型完全相同,而且是設計成與 IIS 服務進行裝載。

下列範例會示範要搭配 Windows 用戶端認證類型使用的繫結。

// The code uses a shortcut to specify the security mode to Transport.
WSHttpBinding b = new WSHttpBinding(SecurityMode.Transport);
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
' The code uses a shortcut to specify the security mode to Transport.
Dim b As WSHttpBinding = New WSHttpBinding(SecurityMode.Transport)
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows

WSDualHttpBinding

這個繫結只會提供訊息層級安全性,而不提供傳輸層級安全性。

NetTcpBinding

NetTcpBinding 類別會使用 TCP 來進行訊息傳輸。 傳輸模式的安全性是由實作透過 TCP 的傳輸層安全性 (TLS) 所提供。 而 TLS 實作則是由作業系統提供。

如下列程式碼所示,您也可以藉由將 ClientCredentialType 類別的 TcpTransportSecurity 屬性設定為其中一個 TcpClientCredentialType 值,來指定用戶端的認證類型。

NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.Transport;
b.Security.Transport.ClientCredentialType =
TcpClientCredentialType.Certificate;
Dim b As NetTcpBinding = New NetTcpBinding()
b.Security.Mode = SecurityMode.Transport
b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate

用戶端

若是使用用戶端類型,您必須指定使用 SetCertificate 類別之 X509CertificateInitiatorClientCredential 方法的憑證。

注意

如果是使用 Windows 安全性,就不需要提供憑證。

下列程式碼範例使用可以提供唯一識別該憑證的憑證指紋。 如需憑證的詳細資訊,請參閱使用憑證

NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.Transport;
b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate;
EndpointAddress a = new EndpointAddress("net.tcp://contoso.com/TcpAddress");
ChannelFactory<ICalculator> cf = new ChannelFactory<ICalculator>(b, a);
cf.Credentials.ClientCertificate.SetCertificate(
    StoreLocation.LocalMachine,
    StoreName.My,
    X509FindType.FindByThumbprint,
    "0000000000000000000000000000000000000000");
Dim b As NetTcpBinding = New NetTcpBinding()
b.Security.Mode = SecurityMode.Transport
b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate
Dim a As New EndpointAddress("net.tcp://contoso.com/TcpAddress")
Dim cf As ChannelFactory(Of ICalculator) = New ChannelFactory(Of ICalculator)(b, a)
cf.Credentials.ClientCertificate.SetCertificate( _
    StoreLocation.LocalMachine, _
    StoreName.My, _
    X509FindType.FindByThumbprint, _
    "0000000000000000000000000000000000000000")

或者,您也可以使用行為區段中的 <clientCredentials> 元素,在用戶端的組態中指定此憑證。

<behaviors>  
  <behavior>  
   <clientCredentials>  
     <clientCertificate findValue= "101010101010101010101010101010000000000"
      storeLocation="LocalMachine" storeName="My"
      X509FindType="FindByThumbPrint">  
     </clientCertificate>  
   </clientCredentials>  
 </behavior>  
</behaviors>

NetNamedPipeBinding

NetNamedPipeBinding 類別已設計成可進行有效率的電腦內部通訊,也就是說,它可以用在執行於同一電腦的處理序,雖然您也可以在同一網路中的兩台電腦之間建立具名管道通道。 這個繫結只能提供傳輸層級安全性。 當建立使用這個繫結的應用程式時,其端點位址必須包含 "net.pipe" 做為端點位址的通訊協定。

WSFederationHttpBinding

使用傳輸安全性時,這個繫結會使用透過 HTTP 的 SSL (即所謂的 HTTPS) 和已發行的權杖 (TransportWithMessageCredential)。 如需同盟應用程式的詳細資訊,請參閱同盟和已發行權杖

NetPeerTcpBinding

NetPeerTcpBinding 類別是設計成可使用對等網路功能來進行有效通訊的安全傳輸。 從類別和繫結名稱可看出,TCP 是一種通訊協定。 當安全性模式設定為傳輸時,繫結會透過 TCP 實作 TLS。 如需對等功能的詳細資訊,請參閱對等網路

MsmqIntegrationBinding 和 NetMsmqBinding

如需訊息佇列 (先前稱為 MSMQ) 和傳輸安全性的完整討論,請參閱使用傳輸安全性來確保訊息的安全

另請參閱