具備傳輸安全性的 BasicBinding

這個範例會示範透過基本繫結來使用 SSL 傳輸安全性。這個範例是以實作計算機服務的使用者入門範例為基礎。

根據預設,基本繫結支援 HTTP 通訊。此範例會示範如何啟用基本繫結的傳輸安全性。在執行範例之前,您必須使用 [Web 伺服器憑證精靈] 建立並指派憑證。

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

本範例中的程式碼與使用者入門範例服務中的程式碼相同。組態檔設定中的端點定義與繫結定義會修改成啟用安全通訊,如下列範例組態所示:

<system.serviceModel>
   <services>
      <service 
          type="Microsoft.ServiceModel.Samples.CalculatorService"
          behaviorConfiguration="CalculatorServiceBehavior">
         <endpoint address=""
               binding="basicHttpBinding"
               bindingConfiguration="Binding1" 
               contract="Microsoft.ServiceModel.Samples.ICalculator" />
      </service>
   </services>
    <bindings>
        <basicHttpBinding>
        <!-- Configure basicHttpBinding with Transport security -- >
        <!-- mode and clientCredentialType set to None.-->
           <binding name="Binding1">
               <security mode="Transport">
                   <transport clientCredentialType="None"/>
               </security>
           </binding>
        </basicHttpBinding>
    </bindings>
    ...
</system.serviceModel>

因為本範例中所使用的憑證是使用 Makecert.exe 所建立的測試憑證,所以當您嘗試在瀏覽器中存取 HTTPS: 位址時 (例如 https://localhost/servicemodelsamples/service.svc),會顯示安全性警示。若要允許 Windows Communication Foundation (WCF) 用戶端使用測試憑證,就必須在用戶端新增某些其他程式碼以便隱藏安全性警示。使用實際憑證時,不需要這個程式碼及伴隨的類別。

// This code is required only for test certificates such as 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. 請確定您已執行 Windows Communication Foundation 範例的單次安裝程序

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

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

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

Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.