共用方式為


WS 傳輸安全性

這個範例會示範搭配 WSHttpBinding 繫結來使用 SSL 傳輸安全性。 根據預設,wsHttpBinding 繫結會提供 HTTP 通訊。 當針對傳輸安全性設定時,此繫結會支援 HTTPS 通訊。 這個範例是以實作計算機服務的使用者入門範例為基礎。 wsHttpBinding 會指定並設定在用戶端和服務的應用程式組態檔中。

ms751427.note(zh-tw,VS.100).gif注意:
此範例的安裝程序與建置指示位於本主題的結尾。

ms751427.Important(zh-tw,VS.100).gif 注意:
這些範例可能已安裝在您的電腦上。 請先檢查下列 (預設) 目錄,然後再繼續。

<InstallDrive>:\WF_WCF_Samples

如果此目錄不存在,請移至用於 .NET Framework 4 的 Windows Communication Foundation (WCF) 與 Windows Workflow Foundation (WF) 範例 (英文),以下載所有 Windows Communication Foundation (WCF) 和 WF 範例。 此範例位於下列目錄。

<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Binding\WS\wsTransportSecurity

本範例中的程式碼與使用者入門範例服務中的程式碼相同。 您必須建立一個憑證並使用 [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 範例的單次安裝程序

  3. 請確定您已執行 網際網路資訊服務 (IIS) 伺服器憑證安裝指示

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

  5. 若要在單一或跨電腦的組態中執行本範例,請遵循Running the Windows Communication Foundation Samples中的指示。