Share via


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

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

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

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

  2. Mode 属性设置为适当的值。 本示例使用 Message 模式。

  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. 添加 <binding> 元素,并将 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>  
    

另请参阅