HttpCookie and web.config domain

You can set in your Asp.Net web.config file a domain for all your cookie. This is done by setting the httpcookies from the system.web.

     <httpCookies domain=""/>

The main goal is to have all your cookies attached to the domain and not set to the subdomain. Also, notice that I have wrote and not This is important, otherwise, it won’t work with subdomain.

So what does it do behind the scene? It simply set the domain property of the cookie to the domain name. In fact you could have code it manually:

var cookie = new HttpCookie();
cookie.Domain = "";

Instead you set it once in the web.config.
This is the constructor of HttpCookie. As you can see, it calls SetDefaultsFromConfig().

public HttpCookie(String name, String value) { 
     _name = name;
     _stringValue = value;
     _changed = true;

This method goes into the web.config to get the domain.

private void SetDefaultsFromConfig() {
    HttpCookiesSection config = RuntimeConfig.GetConfig().HttpCookies; 
    _secure = config.RequireSSL;
    _httpOnly = config.HttpOnlyCookies;

    if (config.Domain != null && config.Domain.Length > 0) 
          _domain = config.Domain;

The variable _domain is changed by the configuration file value.

This variable is also setted by the domain property.

        public String Domain { 
            get { return _domain;}
            set {
                _domain = value;
                _changed = true; 

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

7 thoughts on “HttpCookie and web.config domain

  1. Hola! I’ve been reading your site for some time now and finally got the bravery to go ahead and give you a shout out from Huffman Tx! Just wanted to tell you keep up the excellent job!

  2. Having read this I thought it was very informative.
    I appreciate you finding the time and energy to put this informative article together.

  3. Wonderful web site. Lots of useful information here. I’m sending it to several
    pals ans additionally sharing in delicious. And of course,
    thank you to your sweat!

  4. Great post. I was checking constantly this blog and I am impressed!
    Extremely helpful information specially the last part 🙂 I care for
    such information much. I was looking for this certain info for a very long time.
    Thank you and good luck.

  5. Greetings I am so happy I found your site, I really found you
    by accident, while I was browsing on Askjeeve for something else,
    Anyhow I am here now and would just like to say thanks for
    a incredible post and a all round enjoyable blog (I also love the
    theme/design), I don’t have time to read through it all at
    the minute but I have bookmarked it and also added in your RSS feeds, so when I have time I will be back
    to read much more, Please do keep up the great work.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.