Tuesday, May 10, 2011

Send Cookies When Making WCF Service Calls


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


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.
  • If you are calling a "REST" service using the "WebClient" class, this should not be a difficult task. You can simply work on the "CookieContainer" property of the "HttpWebRequest" class.
  • If you are calling a regular WCF service, and your client proxies are generated by the "Adding service reference" tool in the Visual Studio, the method to send cookies is not so obvious.
This article is to present an example on how to send cookies when making WCF calls using the Visual Studio generated client proxies.


The attached Visual Studio solution is developed in Visual Studio 2010. It has two projects:
  • The "WCFHost" is a simple ASP.NET MVC project that hosts the example WCF service.
  • The "WCFClient" is a simple WPF project. I will show you how to send cookies when calling WCF services in this project.
Let us start by first taking a look at the WCF service.

The WCF Service

The example WCF service is implemented in the "ServiceWithCookies.svc.cs" file in the "WCFHost" project.

See full details: http://www.codeproject.com/KB/WCF/WCFServiceCookie.aspx

No comments: