Introduction

This article presents an example on how to send cookies when making WCF service calls.

Background

Once in a while, you may find that you want to send some information in the form of "Cookies" to the server when making WCF calls. For example, if the WCF service is protected by a "Forms Authentication" mechanism, you will need to send the authentication cookie when making the WCF call to gain the required access to the service.

The "DataContract" "CookieInformation" class has two fields named "Key" and "Value".

The implementation of the "OperationContract" "EchoCookieInformation" reads the cookie from the client and puts the information into an instance of the "CookieInformation" class.

Sending application data to WCF services by cookies is not the "recommended" way of communicating business data. In this example, the whole purpose of reading the cookie from the client is to show you that the WCF client can indeed send cookies to the server. In order that the WCF service method "EchoCookieInformation" has access to the "HttpContext", we need to add the following in the "web.config" file inside the "<system.serviceModel>" tag.

<serviceHostingEnvironmentaspNetCompatibilityEnabled="true"/>

Now we can take a look at how the client sends cookies when making the call.

The WCF Service Client

In order to access the WCF service, I used the "Adding service reference" tool from the Visual Studio to generate the client proxies. The proxies generated do not directly support cookies. To send cookies, I created two utility classes in the "CookieBehavior.cs" file.

The "CookieMessageInspector" class implements the "IClientMessageInspector" interface. In the "BeforeSendRequest" method, the cookie in the form of a string is added to the request message to the WCF service.

The "CookieBehavior" implements the "IEndpointBehavior" interface. In the "ApplyClientBehavior" method, an instance of the "CookieMessageInspector" class is added to the "MessageInspectors" collection.

With the help of these two utility classes, we can then insert the cookies into the client proxies. The "MainWindow.xaml" file is the single window UI of the test program.

In the "Click" event of the button, I first formulated the cookie string. The cookie string to send to the server is simply a key/value pair. If you want to know more about the formatting of the cookie strings, you can take a look at this link. I then initiated a "CookieBehavior" object with the cookie string and associated it with the WCF client. When the WCF call finishes, the information from the call is displayed in a "MessageBox".

Now we can take a look at how the WCF client runs.

Run the Example

Set the "WCFClient" project as the start up project, we can debug run the example. When the application launches, we can see the single button window as the following:

Click on the button and wait until the WCF call finishes, we can see the following message box.

As we can see, both the key and the value of the cookie sent to the service are successfully received by the server and "echoed" back to the client.

Points of Interest

This article presented an example on how to send cookies when making WCF calls.

If you are calling a "REST" service using the "WebClient", you should have some simpler ways to send cookies to the service. The method introduced here is targeting to the case when we are calling a regular WCF service and we are using the "Adding service reference" tool from the Visual Studio to generate the client proxies.

In most cases, we should not have the necessity to send cookies when we make WCF calls. But if the service is behind a "Forms Authentication" mechanism, we will need to send the authentication cookie to gain access to the service.

I hope you like my postings and I hope this article can help you one way or the other.