Home » ASP » HttpCookie and web.config domain

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.

  <system.web>
     <httpCookies domain=".domain.com"/>
  </system.web>

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 .domain.com and not domain.com. 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 = ".domain.com";

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;
     SetDefaultsFromConfig(); 
     _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 Responses so far.

  1. Ruben says:

    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. Web Names says:

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

  3. Christopher says:

    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. china man says:

    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.

  6. Deloras says:

    Excellent web site you have got here.. It’s hard to find
    excellent writing like yours nowadays. I seriously appreciate people like you!
    Take care!!

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.