Thursday, 28 February 2013

Create shared cookie across sub domains (

Let's say you have which writes a cookie. You need that cookie to be accessible by a sub domain of your original site: To enable this, the cookie that is written must not include the specific sub domain in the domain property of the cookie (www). The cookie's domain must be sub domain-less, and begin with a period (.). This then permits the browser to include the cookie in requests to all sub domains, permitting each sub domain to read the cookie content.

Basically, the cookie's domain must go
From this:
To this:

The most obvious way to achieve this is to hard code the domain value when writing the cookie - but that always feels wrong and it doesn't help when running on localhost. Nor is it desirable if you want to change your domain.

The answer I came up with is an extension method that makes use of the requested url. That way, you don't ever have to worry what domain you're running under: you'll always get just the the sub domain safe version. It also takes care of a locally hosted domain. Enjoy.

Edit 1: Ed has pointed out the comments that will read the most explicit cookie first. So to be wary of implementing this approach in a site with already existing cookies that do include the sub domain.

Edit 2: Be wary of this approach when using an integrated cloud hosting provider such as AppHarbor. AH Applications are given a url that varies only in the sub domain on the main AppHarbor domain. E.g the application FooBar on AppHarbor is hosted by default on Using the technique above would allow any other site hosted in AppHarbor so read the client cookie! To mitigate this you can apply your own hostname to the application and make it canonical so that your site cannot be accessed from the original url. Indeed, its also a good reason not to put sensitive information in the cookie!


  1. Nice clean example.

    One thing to be careful of here - will always take the most explicit cookie value first (e.g. on the site '', a cookie for '' will be considered more relevant than '').

    This won't be an issue on fresh sites that always implement this method, but be mindful if converting from a site you already have established - so if you were running with '' with existing users and then implement this code going forward with both the 'www' and 'other' version your users could end up with two cookies which may have some big implications.

    Also remember to change the value in your web.config for any session based cookies written by out of your control.

  2. Thanks Ed, good to know! Will update the post. What's the web.config setting you mention? Sounds out of scope if you're just writing your own cookies.

  3. This comment has been removed by the author.

  4. This comment has been removed by the author.

  5. Oops, HTML tags messed up my previous post:

    Yeah, only relevant for session cookies

    <httpCookies domain=""/>

    Got there in the end!

  6. This line: if (context.Request.IsLocal)

    Is Supposed to be if (!context.Request.IsLocal)

  7. Also: "by design domain names must have at least two dots otherwise browser will say they are invalid"

    You don't want to mess with the domain localhost, has this figured out and you will be able to access cookie across localhost websites already

    // correctly handles localhost so all
    //major browsers will accept cookie
    if (!context.Request.IsLocal)
    var domainSegments = context.Request.Url.Host.Split('.');
    domain = "." + String.Join(".", domainSegments.Skip(1));

    var cookie = new HttpCookie(name, value)
    Domain = domain

    1. Thanks sweetog for pointing out those errors, I've updated the gist.

  8. You can dramatically increase your net worth by increasing your ability to Quavo Net Worth Appreciation is a human emotion and it's no accident that it's very much interlinked with the financial term.