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

ReliableSessionOverHttps 示例演示了将 SSL 传输安全性与 Reliable Sessions 配合使用。 可靠会话实现 WS-Reliable 消息传送协议。 您可以通过在可靠会话上组合 WS-Security 来获得安全的可靠会话。 但有时,可以选择将 HTTP 传输安全性与 SSL 配合使用。

示例详细信息

SSL 可确保数据包本身受到保护。 请务必注意,这与使用 WS-Secure Conversation 保护可靠会话不同。

若要通过 HTTPS 使用可靠会话,必须创建自定义绑定。 此示例基于《入门指南》,实现了计算器服务。 使用可靠会话绑定元素和 <httpsTransport> 创建自定义绑定。 下面是自定义绑定的配置。

<?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 http://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 示例 执行One-Time 安装过程。

  3. 确保已执行 Internet Information Services (IIS) 服务器证书安装说明

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

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