消息安全示例

MessageSecurity 示例演示如何实现使用basicHttpBinding和消息安全性的应用程序。 此示例基于《入门指南》,实现了计算器服务。

注释

本示例的设置过程和生成说明位于本主题末尾。

安全模式 basicHttpBinding 可以设置为以下值: MessageTransportTransportWithMessageCredentialTransportCredentialOnlyNone。 在以下示例服务 App.config 文件中,终结点定义指定 basicHttpBinding 并引用名为的 Binding1绑定配置,如以下示例配置所示:

<system.serviceModel>
  <services>
    <service name="Microsoft.ServiceModel.Samples.CalculatorService"
             behaviorConfiguration="CalculatorServiceBehavior">
     <!-- This endpoint is exposed at the base address provided by -->
     <!-- host: http://localhost:8000/ServiceModelSamples/service.-->
     <endpoint address=""
               binding="basicHttpBinding"
               bindingConfiguration="Binding1"
               contract="Microsoft.ServiceModel.Samples.ICalculator" />
    </service>
  </services>
  ...
</system.serviceModel>

绑定配置将 > 的 属性设置为 Message,将 > 的 属性设置为 Certificate,如下面的示例配置中所示:

<bindings>
  <basicHttpBinding>
    <!--
        This configuration defines the SecurityMode as Message and
        the clientCredentialType as Certificate.
        -->
    <binding name="Binding1" >
      <security mode = "Message">
        <message clientCredentialType="Certificate"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

服务用于向客户端进行身份验证的证书是在元素下的 serviceCredentials 配置文件的行为部分中设置的。 适用于客户端用来向服务进行身份验证的证书的验证模式也在元素下 clientCertificate 的行为部分中设置。

<!--For debugging purposes, set the includeExceptionDetailInFaults attribute to true.-->
<behaviors>
  <serviceBehaviors>
    <behavior name="CalculatorServiceBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="False" />
      <!--The serviceCredentials behavior allows one to define a -->
      <!--service certificate. A service certificate is used by a -->
      <!--client to authenticate the service and provide message -->
      <!-- protection. This configuration references the "localhost"-->
      <!--certificate installed during the setup instructions. -->
      <serviceCredentials>
        <serviceCertificate findValue="localhost"
               storeLocation="LocalMachine"
               storeName="My" x509FindType="FindBySubjectName" />
        <clientCertificate>
          <!-- Setting the certificateValidationMode to -->
          <!-- PeerOrChainTrust means that if the certificate -->
          <!--is in the user's Trusted People store, then it is -->
          <!-- trusted without performing a validation of the -->
          <!-- certificate's issuer chain. This setting is used -->
          <!-- here for convenience so that the sample can be run -->
          <!-- without having to have certificates issued by a -->
          <!-- certification authority (CA). -->
          <!-- This setting is less secure than the default, -->
          <!-- ChainTrust. The security implications of this -->
          <!-- setting should be carefully considered before using -->
          <!-- PeerOrChainTrust in production code. -->
          <authentication
                       certificateValidationMode="PeerOrChainTrust" />
        </clientCertificate>
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>

客户端配置文件中指定了相同的绑定和安全详细信息。

调用方的身份使用以下代码显示在服务控制台窗口中:

Console.WriteLine("Called by {0}", ServiceSecurityContext.Current.PrimaryIdentity.Name);

运行示例时,操作请求和响应将显示在客户端控制台窗口中。 在客户端窗口中按 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 示例 执行One-Time 安装过程。

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

在同一计算机上运行示例

  1. 从示例安装文件夹运行 Setup.bat。 这会安装运行示例所需的所有证书。

    注释

    Setup.bat 批处理文件旨在从 Windows SDK 命令提示符运行。 这要求 MSSDK 环境变量指向 SDK 的安装目录。 此环境变量在 Windows SDK 命令提示符中自动设置。

  2. 从 \service\bin 运行服务应用程序。

  3. 从 \client\bin 运行客户端应用程序。 客户端活动显示在客户端控制台应用程序中。

  4. 如果客户端和服务无法通信,请参阅 WCF 示例 故障排除提示。

  5. 完成示例后,通过运行 Cleanup.bat 来删除证书。 其他安全示例使用相同的证书。

跨计算机运行示例

  1. 在服务计算机上为服务二进制文件创建目录。

  2. 将服务程序文件复制到服务器上的服务目录。 此外,将 Setup.bat、Cleanup.bat和 ImportClientCert.bat 文件复制到服务器。

  3. 在客户端计算机上为客户端二进制文件创建目录。

  4. 将客户端程序文件复制到客户端计算机上的客户端目录。 此外,将 Setup.bat、Cleanup.bat和 ImportServiceCert.bat 文件复制到客户端。

  5. 在服务器上,运行 setup.bat service。 如果采用 setup.bat 参数运行 service,则使用计算机的完全限定域名创建一个服务证书,并将此服务证书导出到名为 Service.cer 的文件中。

  6. 编辑 Service.exe.config,以在 findValue 属性中的 <serviceCertificate> 元素中反映新证书名称,该名称与计算机的完全限定域名相同。 还要更改基址的值以指定一个完全限定的计算机名(而不是 localhost)。.

  7. 将Service.cer文件从服务目录复制到客户端计算机上的客户端目录。

  8. 在客户端上运行 setup.bat client。 使用 setup.bat 参数运行 client 将创建名为 client.com 的客户端证书,并将客户端证书导出到名为Client.cer的文件。

  9. 在客户端计算机上的 Client.exe.config 文件中,更改终结点的地址值以匹配服务的新地址。 通过使用服务器的完全限定域名替换 localhost 来执行此操作。 此外,将 findValue 属性中的 <defaultCertificate> 更改为新的服务证书名称,该名称是服务器的完全限定域名。

  10. 将Client.cer文件从客户端目录复制到服务器上的服务目录。

  11. 在客户端上运行 ImportServiceCert.bat。 这会将服务证书从 Service.cer 文件导入 CurrentUser - TrustedPeople 存储中。

  12. 在服务器上,运行 ImportClientCert.bat,这会将客户端证书从 Client.cer 文件导入 LocalMachine - TrustedPeople 存储。

  13. 在服务计算机上,从命令提示符运行 Service.exe。

  14. 在客户端计算机上,从命令提示符窗口启动 Client.exe。

    1. 如果客户端和服务无法通信,请参阅 WCF 示例 故障排除提示。

运行示例后进行清理

  • 运行完示例后,在示例文件夹中运行 Cleanup.bat。

    注释

    在跨计算机运行此示例时,此脚本不会删除客户端上的服务证书。 如果您运行了在多个计算机上使用证书的 Windows Communication Foundation (WCF) 示例,请务必清除安装在当前用户 - TrustedPeople 存储中的服务证书。 为此,请使用以下命令:例如: certmgr -del -r CurrentUser -s TrustedPeople -c -n <Fully Qualified Server Machine Name>certmgr -del -r CurrentUser -s TrustedPeople -c -n server1.contoso.com