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.
若要安裝、建置及執行範例
請使用下列命令安裝 ASP.NET 4.0。
%windir%\Microsoft.NET\Framework\v4.0.XXXXX\aspnet_regiis.exe /i /enable
請確認已執行網際網路資訊服務 (IIS) 伺服器憑證安裝指示。
若要建置方案的 C# 或 Visual Basic .NET 版本,請遵循 Building the Windows Communication Foundation Samples中的指示。
若要在單一或多部電腦組態中執行此範例,請遵循執行 Windows Communication Foundation 範例中的指示進行。