共用方式為


搭配 WCF 使用多個驗證機制

WCF 現在可讓您在單一端點上指定多個驗證配置。 此外,Web 託管服務可以直接從 IIS 繼承其驗證設定。 自行架設的服務可以指定可以使用哪些驗證方案。 如需在 IIS 中設定驗證設定的詳細資訊,請參閱 IIS 驗證

IIS-Hosted 服務

針對 IIS 裝載的服務,設定您想要在 IIS 中使用的驗證配置。 然後在服務的 web.config 檔案中,在系結組態中,將 clientCredential 類型指定為 “InheritedFromHost”,如下列 XML 代碼段所示:

<bindings>  
      <basicHttpBinding>  
        <binding name="secureBinding">  
          <security mode="Transport">  
            <transport clientCredentialType="InheritedFromHost" />  
          </security>  
        </binding>  
      </basicHttpBinding>  
    </bindings>  

您可以使用 ServiceAuthenticationBehavior 或 <serviceAuthenticationManager> 元素,指定僅用於您的服務的特定驗證配置子集。 在程式代碼中設定此專案時,請使用 ServiceAuthenticationBehavior,如下列代碼段所示。

// ...  
ServiceAuthenticationBehavior sab = null;  
sab = serviceHost.Description.Behaviors.Find<ServiceAuthenticationBehavior>();  
  
if (sab == null)  
{  
    sab = new ServiceAuthenticationBehavior();  
    sab.AuthenticationSchemes = AuthenticationSchemes.Basic | AuthenticationSchemes.Negotiate | AuthenticationSchemes.Digest;  
    serviceHost.Description.Behaviors.Add(sab);  
}  
else  
{  
     sab.AuthenticationSchemes = AuthenticationSchemes.Basic | AuthenticationSchemes.Negotiate | AuthenticationSchemes.Digest;  
}  
// ...  

在組態檔中設定此專案時,請使用 <serviceAuthenticationManager> 元素,如下列 XML 代碼段所示。

<behaviors>  
      <serviceBehaviors>  
        <behavior name="limitedAuthBehavior">  
          <serviceAuthenticationManager authenticationSchemes="Negotiate, Digest, Basic"/>  
          <!-- ... -->  
        </behavior>  
      </serviceBehaviors>  
    </behaviors>  

這將確保在服務端點上套用的驗證機制僅限於此處所列的一部分,具體取決於 IIS 中所選擇的選項。 這表示開發人員可以將基本驗證從 serviceAuthenticationManager 清單中省略,即使它在 IIS 中已啟用,也不會在服務端點上套用。

Self-Hosted 服務

自行託管的服務配置方式稍微不同,因為沒有 IIS 供應設定。 在這裡, <您會使用 serviceAuthenticationManager> 元素或 ServiceAuthenticationBehavior 來指定將繼承的驗證設定。 在程式代碼中,它看起來像這樣:

// ...  
ServiceAuthenticationBehavior sab = null;  
sab = serviceHost.Description.Behaviors.Find<ServiceAuthenticationBehavior>();  
  
if (sab == null)  
{  
    sab = new ServiceAuthenticationBehavior();  
    sab.AuthenticationSchemes = AuthenticationSchemes.Basic | AuthenticationSchemes.Negotiate | AuthenticationSchemes.Digest;  
    serviceHost.Description.Behaviors.Add(sab);  
}  
else  
{  
     sab.AuthenticationSchemes = AuthenticationSchemes.Basic | AuthenticationSchemes.Negotiate | AuthenticationSchemes.Digest;  
}  
// ...  

在設定中,看起來像這樣:

<behaviors>  
      <serviceBehaviors>  
        <behavior name="limitedAuthBehavior">  
          <serviceAuthenticationManager authenticationSchemes="Negotiate, Digest, Basic"/>  
          <!-- ... -->  
        </behavior>  
      </serviceBehaviors>  
    </behaviors>  

然後您可以在系結設定中指定 InheritFromHost,如下列 XML 代碼段所示。

<bindings>  
      <basicHttpBinding>  
        <binding name="secureBinding">  
          <security mode="Transport">  
            <transport clientCredentialType="InheritedFromHost" />  
          </security>  
        </binding>  
      </basicHttpBinding>  
    </bindings>  

或者,您可以在自定義系結中指定驗證配置,方法是在 HTTP 傳輸綁定項上設定驗證配置,如下列組態代碼段所示。

<binding name="multipleBinding">  
      <textMessageEncoding/>  
      <httpTransport authenticationScheme="Negotiate, Ntlm, Digest, Basic" />  
    </binding>  

另請參閱