如何:指定客户端凭据类型

设置安全模式(传输或消息)后,可以选择设置客户端凭据类型。 此属性指定客户端必须提供给服务进行身份验证的凭据类型。 有关设置安全模式的详细信息(设置客户端凭据类型前的必要步骤),请参阅 “如何:设置安全模式”。

在代码中设置客户端凭据类型

  1. 创建服务将使用的绑定的实例。 此示例使用 WSHttpBinding 绑定。

  2. Mode 属性设置为适当的值。 此示例使用消息模式。

  3. ClientCredentialType 属性设置为适当的值。 本示例将它设置为使用 Windows 身份验证(Windows)。

    ServiceHost myServiceHost = new ServiceHost(typeof(CalculatorService));
    // Create a binding to use.
    WSHttpBinding binding = new WSHttpBinding();
    binding.Security.Mode = SecurityMode.Message;
    binding.Security.Message.ClientCredentialType =
        MessageCredentialType.Windows;
    
    Dim myServiceHost As New ServiceHost(GetType(CalculatorService))
    ' Create a binding to use.
    Dim binding As New WSHttpBinding()
    binding.Security.Mode = SecurityMode.Message
    binding.Security.Message.ClientCredentialType = _
    MessageCredentialType.Windows
    

在配置中设置客户端凭据类型

  1. <system.serviceModel> 元素添加到配置文件。

  2. 作为子元素,添加一个 <bindings(绑定)> 元素。

  3. 添加适当的绑定。 此示例使用 <wsHttpBinding> 元素。

  4. <添加绑定>元素并将name属性设置为适当的值。 此示例使用名称“SecureBinding”。

  5. 添加<security>绑定。 将 mode 属性设置为适当的值。 本示例将其设置为 "Message".

  6. 添加一个 <message><transport> 元素,由安全模式确定。 将 clientCredentialType 属性设置为适当的值。 此示例使用 "Windows"

    <system.serviceModel>
      <bindings>
        <wsHttpBinding>
          <binding name="SecureBinding">
            <security mode="Message">
                 <message clientCredentialType="Windows" />
             </security>
          </binding>
        </wsHttpBinding>
      </bindings>
    </system.serviceModel>
    

另见