HOW TO:設定安全性模式

Windows Communication Foundation (WCF) 安全性具有三種常見的安全性模式,可在下列最預先定義的繫結中找到:傳輸、訊息與「使用訊息認證進行傳輸」。另外有兩種額外的模式適用於下列兩種繫結:BasicHttpBinding 上的「僅限傳輸-認證」以及 NetMsmqBinding 上的「兩者並存」模式。然而,此主題將著重在三種常見的安全性模式:TransportMessageTransportWithMessageCredential

請注意,並非所有預先定義的繫結都支援這些模式。本主題將使用 WSHttpBindingNetTcpBinding 類別來設定模式,並示範如何以程式設計方式及透過組態來設定模式。

如需 WCF 安全性的詳細資訊,請參閱安全性概觀保護服務的安全確保服務與用戶端的安全。如需 傳輸模式與訊息的詳細資訊,請參閱傳輸安全性WCF 中的訊息安全性

若要在程式碼中設定安全性模式

  1. 針對您正在使用的繫結類別建立執行個體。如需預先定義之繫結的清單,請參閱系統提供的繫結。下列範例會建立 WSHttpBinding 類別的執行個體。

  2. 針對 Security 屬性傳回的物件,設定其 Mode 屬性。

    另外,如下列程式碼所示,設定訊息的模式。

    或者,如下列程式碼所示,使用訊息認證來設定傳輸的模式。

  3. 您也可以如下列程式碼所示,在繫結的建構函式中設定模式。

設定 ClientCredentialType 屬性

將模式設為三個值中的其中一個值,可決定您設定 ClientCredentialType 屬性的方式。例如,使用 WSHttpBinding 類別並將模式設為 Transport 時,代表您必須將 HttpTransportSecurity 類別的 ClientCredentialType 屬性設為適當值。

若要設定傳輸模式的 ClientCredentialType 屬性

  1. 建立繫結的執行個體。

  2. Mode 屬性設為 Transport

  3. ClientCredential 屬性設定為適當值。下列程式碼會將屬性設為 Windows

若要設定訊息模式的 ClientCredentialType 屬性

  1. 建立繫結的執行個體。

  2. Mode 屬性設為 Message

  3. ClientCredential 屬性設定為適當值。下列程式碼會將屬性設為 Certificate

若要在組態中設定 Mode 與 ClientCredentialType 屬性

  1. 將適當的繫結項目新增至組態檔的 <bindings> 項目中。下列範例會新增 <wsHttpBinding> 項目。

  2. 新增 <binding> 項目,並將它的 name 屬性設為適當值。

  3. 新增 <security> 項目,並將 mode 屬性設為 MessageTransportTransportWithMessageCredential

  4. 如果模式已設為 Transport,則新增 <transport> 項目並將 clientCredential 屬性設為適當值。

    下列範例會將模式設為 "Transport",然後將 <transport> 項目的 clientCredentialType 屬性設為 "Windows"

    <wsHttpBinding>
    <binding name="TransportSecurity">
        <security mode="Transport" />
           <transport clientCredentialType = "Windows" />
        </security>
    </binding>
    </wsHttpBinding >
    

    另外,將 security mode 設為 "Message",並在它後面加上 <"message"> 項目。這個範例會將 clientCredentialType 設為 "Certificate"

    <wsHttpBinding>
    <binding name="MessageSecurity">
        <security mode="Message" />
           <message clientCredentialType = "Certificate" />
        </security>
    </binding>
    </wsHttpBinding >
    

    特殊情況下會使用 TransportWithMessageCredential 值 (請參見下列說明)。

使用 TransportWithMessageCredential

當您將安全性模式設為 TransportWithMessageCredential 時,傳輸會決定用來提供傳輸層安全性的實際機制。例如,HTTP 通訊協定會使用 Secure Sockets Layer (SSL) over HTTP (HTTPS)。因此,會忽略對任何傳輸安全性物件 (例如 HttpTransportSecurity) 的 ClientCredentialType 屬性設定。換句話說,您只能設定訊息安全性物件的 ClientCredentialType (對 WSHttpBinding 繫結來說,指的是 NonDualMessageSecurityOverHttp 物件)。

如需詳細資訊,請參閱 HOW TO:使用傳輸安全性和訊息認證

請參閱

工作

HOW TO:使用 SSL 憑證設定連接埠
HOW TO:使用傳輸安全性和訊息認證

概念

WCF 中的訊息安全性
安全性概觀
系統提供的繫結

其他資源

傳輸安全性
<security> of <wsHttpBinding>
<security> of <basicHttpBinding>
<security> of <netTcpBinding>