WCF常用安全方案(六)

匿名客户端的消息安全

下面的方案演示通过 Windows Communication Foundation (WCF) 消息安全进行保护的客户端和服务。其设计目的是使用消息安全而非传输安全,以便将来可支持更加丰富的基于声明的模型。有关 将丰富声明用于授权的更多信息,请参见使用标识模型管理声明和授权

有关示例应用程序,请参见Message Security Anonymous

匿名客户端的消息安全

特征 说明

安全模式

消息

互操作性

仅 WCF

身份验证(服务器)

初始协商要求服务器身份验证,而不是客户端身份验证

身份验证(客户端)

完整性

是,使用共享安全上下文

保密性

是,使用共享安全上下文

传输

HTTP

服务

下面的代码和配置应独立运行。执行下列操作之一:

  • 使用代码(而不使用配置)创建独立服务。
  • 使用提供的配置创建服务,但不定义任何终结点。

代码

下面的代码演示如何创建使用消息安全的服务终结点。

配置

以下配置可代替代码使用。服务行为元素用于指定用来向客户端验证服务身份的证书。服务元素必须使用 behaviorConfiguration 属性指定行为。绑定元素指定客户端凭据类型为 None,这将允许匿名客户端使用该服务。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceCredentialsBehavior">
          <serviceCredentials>
            <serviceCertificate findValue="contoso.com" 
                                storeLocation="LocalMachine"
                                storeName="My" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="ServiceCredentialsBehavior" 
               name="ServiceModel.Calculator">
        <endpoint address="http://localhost/Calculator" 
                  binding="wsHttpBinding"
                  bindingConfiguration="WSHttpBinding_ICalculator" 
                  name="CalculatorService"
                  contract="ServiceModel.ICalculator" />
      </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ICalculator" >
          <security mode="Message">
            <message clientCredentialType="None" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client />
  </system.serviceModel>
</configuration>

客户端

下面的代码和配置应独立运行。请执行下列操作之一:

  • 使用代码(和客户端代码)创建独立客户端。
  • 创建不定义任何终结点地址的客户端。而使用将配置名称作为参数的客户端构造函数。例如:

代码

下面的代码创建客户端的一个实例。绑定使用消息模式安全,客户端凭据类型设置为 None。

配置

下面的代码将配置客户端。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ICalculator" >
          <security mode="Message">
            <message clientCredentialType="None" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://machineName/Calculator"
        binding="wsHttpBinding"
        bindingConfiguration="WSHttpBinding_ICalculator" 
        contract="ICalculator"
        name="WSHttpBinding_ICalculator">
        <identity>
          <dns value="contoso.com" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>
posted @ 2009-12-11 13:37  kim zeng  阅读(281)  评论(0编辑  收藏  举报