基于 HTTPS 的自定义绑定可靠会话

此示例演示对可靠会话使用 SSL 传输安全。可靠会话实现 WS-Reliable Messaging 协议。您可以通过在可靠会话上组合 WS-Security 来获得安全的可靠会话。但是有时,您可以选择将 HTTP 传输安全用于 SSL。

Aa395197.Important(zh-cn,VS.100).gif 注意:
您的计算机上可能已安装这些示例。在继续操作之前,请先检查以下(默认)目录:

<安装驱动器>:\WF_WCF_Samples

如果此目录不存在,请访问针对 .NET Framework 4 的 Windows Communication Foundation (WCF) 和 Windows Workflow Foundation (WF) 示例(可能为英文网页),下载所有 Windows Communication Foundation (WCF) 和 WF 示例。此示例位于以下目录。

<安装驱动器>:\WF_WCF_Samples\WCF\Basic\Binding\Custom\ReliableSessionOverHttps

示例详细信息

SSL 可确保数据包本身是安全的。需要特别注意的是,这与使用 WS-Secure Conversation 来保证可靠会话的安全是不同的。

若要使用基于 HTTPS 的可靠会话,必须创建自定义绑定。此示例基于实现计算器服务的入门示例。可以使用可靠会话绑定元素和 httpsTransport element创建自定义绑定。下面是自定义绑定的配置。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service 
          name="Microsoft.ServiceModel.Samples.CalculatorService"
          behaviorConfiguration="CalculatorServiceBehavior">
        <!-- use base address provided by host -->
        <endpoint address=""
                  binding="customBinding"
                  bindingConfiguration="reliableSessionOverHttps" 
                  contract="Microsoft.ServiceModel.Samples.ICalculator" />
        <!-- the mex endpoint is exposed as https://localhost/servicemodelsamples/service.svc/mex-->
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange"/>
      </service>
    </services>

    <bindings>
      <customBinding>
        <binding name="reliableSessionOverHttps">
          <reliableSession />
          <httpsTransport />
        </binding>
      </customBinding>
    </bindings>
    
    <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>

</configuration>

此示例中的程序代码与入门示例服务的程序代码相同。必须在生成和运行示例之前使用 Web 服务器证书向导创建证书并分配此证书。配置文件设置中的终结点定义和绑定定义允许使用自定义绑定,如下面的客户端示例配置所示。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>

    <client>
      <!-- this endpoint has an https: address -->
      <endpoint name=""
                address="https://localhost/servicemodelsamples/service.svc" 
                binding="customBinding" 
                bindingConfiguration="reliableSessionOverHttps" 
                contract="Microsoft.ServiceModel.Samples.ICalculator" />
    </client>

      <bindings>
        <customBinding>
          <binding name="reliableSessionOverHttps">
            <reliableSession />
            <httpsTransport />
          </binding>
        </customBinding>      
    </bindings>

  </system.serviceModel>

</configuration>

指定的地址使用 https:// 方案。

因为此示例中使用的证书是用 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. 请确保已执行 Internet Information Services (IIS) 服务器证书安装说明

  4. 若要生成 C# 或 Visual Basic .NET 版本的解决方案,请按照生成 Windows Communication Foundation 示例中的说明进行操作。

  5. 若要用单机配置或跨计算机配置来运行示例,请按照Running the Windows Communication Foundation Samples中的说明进行操作。