如何指定客户端凭据值

使用 Windows Communication Foundation (WCF),服务可以指定客户端如何向服务进行身份验证。 例如,服务可以规定使用证书对客户端进行身份验证。

确定客户端凭据类型

  1. 从服务的元数据终结点检索元数据。 元数据通常由两个文件组成:采用所选编程语言(默认值为 Visual C#)和 XML 配置文件的客户端代码。 检索元数据的一种方法是使用 Svcutil.exe 工具返回客户端代码和客户端配置。 有关详细信息,请参阅检索元数据ServiceModel 元数据实用工具工具(Svcutil.exe)。

  2. 打开 XML 配置文件。 如果使用 Svcutil.exe 工具,则文件的默认名称 Output.config。

  3. 查找具有属性的安全元素(安全模式 =其中 设置为某个安全模式)。

  4. 查找与模式值匹配的子元素。 例如,如果模式设置为 Message,则查找安全<元素中包含的>消息<元素>

  5. 请注意分配给 clientCredentialType 属性的值。 实际值取决于使用哪种模式、传输或消息。

以下 XML 代码显示了使用消息安全性的客户端的配置,并要求证书对客户端进行身份验证。

<security mode="Message">
    <transport clientCredentialType="Windows" proxyCredentialType="None"
        realm="" />
     <message clientCredentialType="Certificate" negotiateServiceCredential="true"
    algorithmSuite="Default" establishSecurityContext="true" />
</security>

示例:将证书作为客户端凭据的 TCP 传输模式

本示例将安全模式设置为传输模式,并将客户端凭据值设置为 X.509 证书。 以下过程演示如何在代码和配置中在客户端上设置客户端凭据值。 这假定你已使用 ServiceModel 元数据实用工具工具(Svcutil.exe) 从服务返回元数据(代码和配置)。 有关详细信息,请参阅 “如何:创建客户端”。

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

  1. 使用 ServiceModel 元数据实用工具工具(Svcutil.exe) 从服务生成代码和配置。

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

  3. 在客户端类上,将ClientCredentialsClientBase<TChannel>类的属性设置为适当的值。 此示例使用 X509CertificateInitiatorClientCredential 类的 SetCertificate 方法将属性设置为 X.509 证书。

    // Create a binding using Transport and a certificate.
    NetTcpBinding b = new NetTcpBinding();
    b.Security.Mode = SecurityMode.Transport;
    b.Security.Transport.ClientCredentialType =
        TcpClientCredentialType.Certificate;
    
    // Create an EndPointAddress.
    EndpointAddress ea = new EndpointAddress(
        "net.tcp://localHost:8036/Calculator/MyCalculator");
    
    // Create the client.
    CalculatorClient cc = new CalculatorClient(b, ea);
    
    // Set the certificate for the client.
    cc.ClientCredentials.ClientCertificate.SetCertificate(
        StoreLocation.LocalMachine,
        StoreName.My,
        X509FindType.FindBySubjectName,
        "cohowinery.com");
    try
    {
        cc.Open();
        // Begin using the client.
        Console.WriteLine(cc.Divide(1001, 2));
        cc.Close();
    }
    catch (AddressAccessDeniedException adExc)
    {
        Console.WriteLine(adExc.Message);
        Console.ReadLine();
    }
    catch (System.Exception exc)
    {
        Console.WriteLine(exc.Message);
        Console.ReadLine();
    }
    
    ' Create a binding using Transport and a certificate.
    Dim b As New NetTcpBinding()
    b.Security.Mode = SecurityMode.Transport
    b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate
    
    ' Create an EndPointAddress.
    Dim ea As New EndpointAddress("net.tcp://localHost:8036/Calculator/MyCalculator")
    
    ' Create the client.
    Dim cc As New CalculatorClient(b, ea)
    
    ' Set the certificate for the client.
    cc.ClientCredentials.ClientCertificate.SetCertificate( _
    StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, "cohowinery.com")
    Try
        cc.Open()
        ' Begin using the client.
        Console.WriteLine(cc.Divide(1001, 2))
        cc.Close()
    Catch adExc As AddressAccessDeniedException
        Console.WriteLine(adExc.Message)
        Console.ReadLine()
    Catch exc As System.Exception
        Console.WriteLine(exc.Message)
        Console.ReadLine()
    End Try
    

    可以使用 X509FindType 类的任何枚举。 在证书更改(例如到期)后,可以使用主题名称作为备用。 使用主题名称使得基础结构能够再次查找证书。

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

  1. <behavior>元素添加到<behaviors>元素中。

  2. <clientCredentials> 元素添加到 <行为> 元素。 请务必将所需 name 属性设置为适当的值。

  3. <clientCertificate> 元素添加到 <clientCredentials> 元素。

  4. 将以下属性设置为适当的值:storeLocation、、storeNamex509FindType、和findValue,如以下代码所示。 有关证书的详细信息,请参阅 “使用证书”。

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

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

另请参阅