Microsoft .NET - Tips and Tricks



Friday, February 23, 2007
Sharing cookies between sub-domains
The challenge

Today we had a new challenge while working on one of new .Net projects. The thing is that we needed to exchange a cookie between two applications that are hosted on same domain but on different sub-domains. Simple sharing between sub-domains does not work by default – if you create a cookie in one application you will not be able to access it from the other application hosted on some other sub-domain.


How it works in production environment

Solution to this problem is actually quite simple when applications run on actual domain and not in development environment. For example, if you have two applications hosted on domain “mydomain.com” under sub-domains “app1.mydomain.com” and “app2.mydomain.com” and you want application1 to create a cookie that application2 will be able to read you can do it like this:

1. In application 1 create a cookie like this:


HttpCookie cookie = new HttpCookie("app1cookie", "val");
cookie.Domain = ".mydomain.com";
response.Cookies.Add(cookie);


Notice the value of Domain property. It is set to “.mydomain.com”. The dot in front of the domain name will allow the cookie to be shared between all sub-domains in domain “mydomain.com”.


2. In application 2 access the cookie like this:


string cookieValue = request.Cookies["app1cookie"].Value;


Problem in development environment

The thing is that we needed to implement this feature and use it also in development environment. To simulate production environment we did next thing:

1. Added two entries to hosts file (you can find it here: c:\WINDOWS\system32\drivers\etc\hosts):


127.0.0.1 app1.projectname.local
127.0.0.1 app2.projectname.local


2. Set “Project URL” parameter for application 1 web project to: http://app1.projectname.local/webapplication1name like on image below:




3. Set “Project URL” parameter for application 2 web project to:
http://app2.projectname.local/webapplication2name


4. Create a cookie in application 1 like this:


HttpCookie cookie = new HttpCookie("app1cookie", "val");
cookie.Domain = ".projectname.local";
response.Cookies.Add(cookie);


5. In application 2 access the cookie like this:


string cookieValue = request.Cookies["app1cookie"].Value;


When you now start application 1 from Visual Studio 2005 startup page will be opened under: http://app1.projectname.local/webapplication1name/startuppage.aspx and application 2 will be able to access the cookie that application 1 created.



What is also important to notice here is that in examples above I used hard-coded value for domain name but that was only for demonstration. In real life projects you must define that value in Web.config file and later when application is going to be deployed to staging/production server you will replace the value with actual domain name.

posted by Popovic Sasa
 

0 Comments:


Post a Comment
+ Blog Home
 

Links

Get the Levi9 RSS Feed
Levi9 Global Sourcing

Previous Posts

Archives

February 2007
March 2007
April 2007
January 2008
February 2008

Powered by Blogger
Levi9 Global Sourcing | Jan van Goyenkade 8 | 1075 HP Amsterdam | The Netherlands | +31(0)72 58 210 55 | info@levi9.com
Copyright © Levi 9 | Design by A36