wsTransportSecurity 示例演示了将 SSL 传输安全性与WSHttpBinding绑定结合使用。 默认情况下,绑定 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
确保已为 Windows Communication Foundation 示例 执行One-Time 安装过程。
若要生成解决方案的 C# 或 Visual Basic .NET 版本,请按照 生成 Windows Communication Foundation 示例中的说明进行操作。
若要在单台计算机或跨计算机配置中运行示例,请按照 运行 Windows Communication Foundation 示例中的说明进行操作。