共用方式為


WS 傳輸安全性

wsTransportSecurity 範例示範如何搭配WSHttpBinding系結使用 SSL 傳輸安全性。 通常,wsHttpBinding 綁定會提供 HTTP 通訊。 設定傳輸安全性時,系結支援 HTTPS 通訊。 這個範例是以實作計算機服務的使用者入門範例為基礎。 wsHttpBinding在用戶端與服務的應用程式群組態檔中指定與設定 。

備註

此範例的設定程式和建置指示位於本主題結尾。

範例中的程式代碼與 用戶入門 服務的程式代碼相同。 您必須先建立憑證,並使用 [Web 伺服器證書精靈] 加以指派,才能建置和執行範例。 組態檔設定中的端點定義和系結定義會啟用 Transport 安全性模式,如下列用戶端的範例組態所示。

<system.serviceModel>

    <client>
      <!-- this endpoint has an https: address -->
      <endpoint address="https://localhost/servicemodelsamples/service.svc" binding="wsHttpBinding" bindingConfiguration="Binding1" contract="Microsoft.Samples.TransportSecurity.ICalculator"/>
    </client>

    <bindings>
      <wsHttpBinding>
        <!-- configure wsHttpbinding with Transport security mode
                   and clientCredentialType as None -->
        <binding name="Binding1">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

  </system.serviceModel>

指定的位址會使用 配置 https:// 。 繫結組態會將安全性模式設定為 Transport。 服務 Web.config 檔案中必須指定相同的安全性模式。

因為此範例中使用的憑證是使用 Makecert.exe建立的測試憑證,因此當您嘗試從瀏覽器存取 HTTPs:位址,例如 https://localhost/servicemodelsamples/service.svc時,會出現安全性警示。 若要允許 Windows Communication Foundation (WCF) 用戶端就地使用測試憑證,已將一些額外的程式代碼新增至用戶端,以隱藏安全性警示。 使用生產憑證時,不需要此程式代碼和隨附類別。

// This code is required only for test certificates like those created by Makecert.exe.
PermissiveCertificatePolicy.Enact("CN=ServiceModelSamples-HTTPS-Server");

當您執行範例時,作業要求和回應會顯示在用戶端控制台視窗中。 在客戶端視窗中按 ENTER 鍵以關閉用戶端。

Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714

Press <ENTER> to terminate client.

要設定、建置和執行範例,請執行以下步驟:

  1. 請使用下列命令安裝 ASP.NET 4.0。

    %windir%\Microsoft.NET\Framework\v4.0.XXXXX\aspnet_regiis.exe /i /enable
    
  2. 請確定您已針對 Windows Communication Foundation 範例 執行One-Time 安裝程式。

  3. 請確定您已執行 Internet Information Services (IIS) 伺服器證書安裝指示

  4. 若要建置解決方案的 C# 或 Visual Basic .NET 版本,請遵循建置 Windows Communication Foundation 範例 中的指示。

  5. 若要在單一或跨計算機組態中執行範例,請遵循執行 Windows Communication Foundation 範例 中的指示。