How to: Set the Security Mode
Windows Communication Foundation (WCF) security has three common security modes that are found on most predefined bindings: transport, message, and "transport with message credential." Two additional modes are specific to two bindings: the "transport-credential only" mode found on the BasicHttpBinding, and the "Both" mode, found on the NetMsmqBinding. However, this topic concentrates on the three common security modes: Transport, Message, and TransportWithMessageCredential.
Note that not every predefined binding supports all of these modes. This topic sets the mode with the WSHttpBinding and NetTcpBinding classes and demonstrates how to set the mode both programmatically and through configuration.
For more information about WCF security, see Security Overview, Securing Services, and Securing Services and Clients. For more information about transport mode and message, see Transport Security and Message Security in WCF.
To set the security mode in code
Create an instance of the binding class that you are using. For a list of predefined bindings, see System-Provided Bindings. This example creates an instance of the WSHttpBinding class.
Set the Mode property of the object returned by the Security property.
Alternatively, set the mode to message, as shown in the following code.
Or set the mode to transport with message credentials, as shown in the following code.
You can also set the mode in the constructor of the binding, as shown in the following code.
Setting the ClientCredentialType Property
Setting the mode to one of the three values determines how you set the ClientCredentialType property. For example, using the WSHttpBinding class, setting the mode to Transport means you must set the ClientCredentialType property of the HttpTransportSecurity class to an appropriate value.
To set the ClientCredentialType property for Transport mode
Create an instance of the binding.
Set the Mode property to Transport.
Set the ClientCredential property to an appropriate value. The following code sets the property to Windows.
To set the ClientCredentialType property for Message mode
Create an instance of the binding.
Set the Mode property to Message.
Set the ClientCredential property to an appropriate value. The following code sets the property to Certificate.
To set the Mode and ClientCredentialType property in configuration
<binding>element and set its name attribute to an appropriate value.
<security>element and set the mode attribute to Message, Transport, or TransportWithMessageCredential.
If the mode is set to Transport, add a
<transport>element and set the clientCredential attribute to an appropriate value.
The following example sets the mode to "
Transport", and then sets the
clientCredentialTypeattribute of the
<transport>element to "
<wsHttpBinding> <binding name="TransportSecurity"> <security mode="Transport" /> <transport clientCredentialType = "Windows" /> </security> </binding> </wsHttpBinding >
Alternatively, set the
security modeto "
Message", followed by a
<"message">element. This example sets the
<wsHttpBinding> <binding name="MessageSecurity"> <security mode="Message" /> <message clientCredentialType = "Certificate" /> </security> </binding> </wsHttpBinding >
Using the TransportWithMessageCredential value is a special case, and is explained below.
When setting the security mode to TransportWithMessageCredential, the transport determines the actual mechanism that provides the transport-level security. For example, the HTTP protocol uses Secure Sockets Layer (SSL) over HTTP (HTTPS). Therefore, setting the ClientCredentialType property of any transport security object (such as HttpTransportSecurity) is ignored. In other words, you can only set the ClientCredentialType of the message security object (for the WSHttpBinding binding, the NonDualMessageSecurityOverHttp object).
For more information, see How to: Use Transport Security and Message Credentials.
TasksHow to: Configure a Port with an SSL Certificate
How to: Use Transport Security and Message Credentials
ConceptsMessage Security in WCF
Other ResourcesTransport Security
<security> of <wsHttpBinding>
<security> of <basicHttpBinding>
<security> of <netTcpBinding>
© 2007 Microsoft Corporation. All rights reserved.
Build Date: 2009-08-07