Edit

Share via


How to: Create a SecurityBindingElement for a Specified Authentication Mode

Windows Communication Foundation (WCF) provides several modes by which clients and services authenticate to one another. You can create security binding elements for these authentication modes by using static methods on the SecurityBindingElement class or through configuration, as shown in the following example.

For more information about the 18 authentication modes, see SecurityBindingElement Authentication Modes.

Example

The following code example shows methods for creating bindings for the various authentication modes.

Note

Once an instance of the SecurityBindingElement object is created, a number of properties are immutable, such as KeyType and MessageSecurityVersion. Calling set on such properties does not change them.

// These public methods create custom bindings based on the built-in
// authentication modes that use the static methods of
// the System.ServiceModel.Channels.SecurityBindingElement class.
public static Binding CreateAnonymousForCertificateBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.
        CreateAnonymousForCertificateBindingElement());
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpTransportBindingElement());
    return new CustomBinding(bec);
}

public static Binding CreateAnonymousForSslNegotiatedBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.
        CreateSslNegotiationBindingElement(false));
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpTransportBindingElement());
    return new CustomBinding(bec);
}

public static Binding CreateCertificateOverTransportBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.
        CreateCertificateOverTransportBindingElement());
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpsTransportBindingElement());
    return new CustomBinding(bec);
}

public static Binding CreateIssuedTokenBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.
        CreateIssuedTokenBindingElement(
        new IssuedSecurityTokenParameters()));
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpTransportBindingElement());
    return new CustomBinding(bec);
}

public static Binding CreateIssuedTokenForCertificateBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.
        CreateIssuedTokenForCertificateBindingElement(
        new IssuedSecurityTokenParameters()));
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpTransportBindingElement());
    return new CustomBinding(bec);
}

public static Binding CreateIssuedTokenForSslNegotiatedBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.
        CreateIssuedTokenForSslBindingElement(
        new IssuedSecurityTokenParameters()));
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpTransportBindingElement());
    return new CustomBinding(bec);
}

public static Binding CreateIssuedTokenOverTransportBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.
        CreateIssuedTokenOverTransportBindingElement(
        new IssuedSecurityTokenParameters()));
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpsTransportBindingElement());
    return new CustomBinding(bec);
}

public static Binding CreateKerberosBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.CreateKerberosBindingElement());
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpTransportBindingElement());
    return new CustomBinding(bec);
}

public static Binding CreateKerberosOverTransportBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.
        CreateKerberosOverTransportBindingElement());
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpsTransportBindingElement());
    return new CustomBinding(bec);
}

public static Binding CreateMutualCertificateBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.
        CreateMutualCertificateBindingElement());
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpTransportBindingElement());
    return new CustomBinding(bec);
}

public static Binding CreateMutualCertificateDuplexBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.
        CreateMutualCertificateDuplexBindingElement());
    bec.Add(new CompositeDuplexBindingElement());
    bec.Add(new OneWayBindingElement());
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpTransportBindingElement());
    return new CustomBinding(bec);
}

public static Binding CreateMutualSslNegotiatedBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.
        CreateSslNegotiationBindingElement(true));
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpTransportBindingElement());
    return new CustomBinding(bec);
}

public static Binding CreateSecureConversationBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.
        CreateSecureConversationBindingElement(
        SecurityBindingElement.CreateSspiNegotiationBindingElement()));
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpTransportBindingElement());
    return new CustomBinding(bec);
}

public static Binding CreateSspiNegotiatedBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.CreateSspiNegotiationBindingElement());
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpTransportBindingElement());
    return new CustomBinding(bec);
}

public static Binding CreateSspiNegotiatedOverTransportBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.
        CreateSspiNegotiationOverTransportBindingElement());
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpsTransportBindingElement());
    return new CustomBinding(bec);
}

public static Binding CreateUserNameForCertificateBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.
        CreateUserNameForCertificateBindingElement());
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpTransportBindingElement());
    return new CustomBinding(bec);
}

public static Binding CreateUserNameForSslNegotiatedBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.CreateUserNameForSslBindingElement());
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpTransportBindingElement());
    return new CustomBinding(bec);
}

public static Binding CreateUserNameOverTransportBinding()
{
    BindingElementCollection bec = new BindingElementCollection();
    bec.Add(SecurityBindingElement.
        CreateUserNameOverTransportBindingElement());
    bec.Add(new TextMessageEncodingBindingElement());
    bec.Add(new HttpsTransportBindingElement());
    return new CustomBinding(bec);
}

See also