如何:指定客户端凭据值

使用 Windows Communication Foundation (WCF),可以指定客户端如何向服务进行身份验证。例如,可以规定客户端使用证书向服务进行身份验证。本示例将安全模式设置为“传输”,将客户端凭据类型设置为“证书”。

该过程出现在三个部分:

  1. 服务开发人员指定客户端必须提供的凭据类型**。
  2. 客户端开发人员使用 ServiceModel Metadata Utility Tool (Svcutil.exe) 向服务查询其元数据(服务客户端代码和配置文件)。
  3. 客户端开发人员创建客户端应用程序并在代码或配置中提供“凭据值”**,该值匹配元数据中规定的凭据类型。

服务代码

在代码中指定服务上的客户端凭据类型

  1. 向代码中添加下列引用。

  2. 创建一个绑定实例。本示例使用 NetTcpBinding

  3. 根据需要设置安全模式。本示例将模式设置为 Transport

  4. 根据需要,将 ClientCredentialType 设置为凭据。本示例将该类型设置为凭据。

  5. 使用构造函数中的服务类型创建 ServiceHost 的一个实例。为简单起见,此处不显示用于演示服务接口及其实现的代码。

  6. 设置服务的凭据值。本示例使用 X509CertificateRecipientServiceCredential 类的 SetCertificate 方法。

在配置中指定服务上的客户端凭据类型

  1. <bindings> 元素中添加相应的绑定。本示例使用 <netTcpBinding Element>

  2. <security> of <netTcpBinding> 添加到绑定中。请确保将必需的 name 属性设置为适当的值。

  3. mode 属性设置为适当的值。本示例使用 "Message" 模式。

  4. 根据需要,向安全绑定中添加传输或消息元素。本示例使用 <message> element of <netTcpBinding>

  5. clientCredential 属性设置为所需的凭据类型。本示例使用 "Certificate"

    <bindings>
      <netTcpBinding>
        <binding name="TcpBindingWithCredential">
          <security mode="Message">
            <message clientCredentialType="Certificate" />
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    
  6. 在创建终结点元素时,请使用 bindingConfiguration 属性值指定绑定配置,如下面的代码中所示。

    <endpoint address="myEndpointAddress"
              binding="netTcpBinding"
              bindingConfiguration="TcpBindingWithCredential"
                 name="netTcpBinding_ICalculator"
              contract="Microsoft.ServiceModel.Samples.ICalculator" />
    

客户端代码

下面的过程演示如何在代码和配置中设置客户端上的客户端凭据值。这里假定您已使用 ServiceModel Metadata Utility Tool (Svcutil.exe) 从服务返回元数据(代码和配置)。有关更多信息,请参见 如何:创建 Windows Communication Foundation 客户端

在代码中指定客户端上的客户端凭据值

  1. 使用 ServiceModel Metadata Utility Tool (Svcutil.exe) 从服务生成代码和配置。

  2. 使用生成的代码创建 WCF 客户端的实例。

  3. 在客户端类上,将 ClientBase 类的 ClientCredentials 属性设置为适当的值。本示例使用 X509CertificateInitiatorClientCredential 类的 SetCertificate 方法将该属性设置为 X.509 证书。

    可以使用 X509FindType 类的任何枚举。如果该证书发生更改(由于超过到期日期),则在此处使用主题名称。使用主题名称,基础结构可以再次查找该证书。

在配置中指定客户端上的客户端凭据值

  1. <behaviors> 元素中添加一个 <behavior> of <endpointBehaviors> 元素。

  2. <behaviors> 元素中添加一个 <serviceCredentials> 元素。请确保将必需的 name 属性设置为适当的值。

  3. <clientCredentials> 元素中添加一个 <clientCertificate> of <serviceCredentials> 元素。

  4. 将下列属性设置为适当的值:storeLocation, storeNamex509FindTypefindValue,如下面的代码中所示。有关 证书的更多信息,请参见使用证书

    <behaviors>
       <endpointBehaviors>
          <behavior name="endpointCredential">
            <clientCredentials>
              <clientCertificate findValue="Contoso.com" 
                                 storeLocation="LocalMachine"
                                 storeName="TrustedPeople"
                                 x509FindType="FindBySubjectName" />
            </clientCredentials>
          </behavior>
       </endpointBehaviors>
    </behaviors>
    
  5. 在配置客户端时,通过设置 <endpoint> 元素的 behaviorConfiguration 属性来指定该行为,如下面的代码中所示。终结点元素是 <client> 元素的子元素。同时还要通过将 bindingConfiguration 属性设置为客户端的绑定来指定绑定配置的名称。如果使用的是生成的配置文件,则自动生成该绑定的名称。在本示例中,该名称为 "tcpBindingWithCredential"

    <client>
      <endpoint name =""
                address="net.tcp://contoso.com:8036/aloha"
                binding="netTcpBinding"
                bindingConfiguration="tcpBindingWithCredential"
                behaviorConfiguration="credentialBehavior" />
    </client>
    

另请参见

任务

如何:创建 Windows Communication Foundation 客户端

参考

NetTcpBinding
SetCertificate
X509CertificateRecipientServiceCredential
ClientBase
X509CertificateInitiatorClientCredential

概念

WCF 安全编程
选择凭据类型
使用证书

其他资源

ServiceModel Metadata Utility Tool (Svcutil.exe)
<netTcpBinding>
<security> of <netTcpBinding>
<message> element of <netTcpBinding>
<behavior> of <endpointBehaviors>
<behaviors>
<clientCertificate> of <serviceCredentials>
<clientCredentials>