Export (0) Print
Expand All

Using Multiple Authentication Schemes with WCF

.NET Framework 4.5

WCF now allows you to specify multiple authentication schemes on a single endpoint. Furthermore web hosted services can inherit their authentication settings directly from IIS. Self-hosted services can specify what authentication schemes can be used. For more information about setting authentication settings in IIS, see IIS Authentication

For IIS-hosted services, set the authentication schemes you wish to use in IIS. Then in your service’s web.config file, in your binding configuration specify clientCredential type as “InheritedFromHost” as shown in the following XML snippet:

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

You can specify that you only want a subset of authentication schemes to be used with your service using the ServiceAuthenticationBehavior or the <serviceAuthenticationManager> element. When configuring this in code use the ServiceAuthenticationBehavior as shown in the following code snippet.

// ...
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;
}
// ...

When configuring this in a config file, use the <serviceAuthenticationManager> element as shown in the following XML snippet.

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

This will ensure that only a subset of the authentication schemes listed here will be considered for applying on the service endpoint, depending on what is selected in the IIS. This means that a developer can exclude say Basic auth from the list by omitting it from the serviceAuthenticationManager listing and even if it is enabled in IIS, it will not be applied on the service endpoint

Self-hosted services are configured a bit differently since there is no IIS to inherit settings from. Here you use the <serviceAuthenticationManager> element or ServiceAuthenticationBehavior to specify the authentication settings that will be inherited. In code it looks like this:

// ...
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;
}
// ...

In config, it looks like this:

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

And then you can specify InheritFromHost in your binding settings as shown in the following XML snippet.

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

Alternatively, you can specify the authentication schemes in a custom binding, by setting the authentication schemes on the HTTP transport binding element, as shown in the following config snippet.

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

Show:
© 2014 Microsoft