作法:指定用戶端認證類型

在設定過安全性模式 (傳輸或訊息) 後,您就會擁有設定用戶端認證類型的選項。 這個屬性會指定用戶端必須提供給服務以進行驗證的認證類型。 如需設定安全性模式 (設定用戶端認證類型之前的必要步驟) 的詳細資訊,請參閱操作說明:設定安全性模式

透過程式碼設定用戶端認證類型

  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. 新增 <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>  
    

另請參閱