title: Cookies with and without the Domain Specified (browser inconsistency)
body: <p>I've noticed that there are some real inconsistencies between browsers in terms of cookies.</p>
  
  <p>This is going to be rather long so bear with me.</p>
  
  <p><strong>Note:</strong> I've setup a domain in my host file called "testdomain.com", this <em>bug</em> WONT work when using "localhost".</p>
  
  <p><strong>Note2:</strong> I am curious to know how this works on Apache/PHP if when you retrieve a cookie by name if it gives a collection of cookies back. </p>
  
  <h2>Wikipedia</h2>
  
  <p>Wikipedia states that: <a href="http://en.wikipedia.org/wiki/HTTP_cookie#Domain_and_Path" rel="noreferrer">http://en.wikipedia.org/wiki/HTTP_cookie#Domain_and_Path</a></p>
  
  <blockquote>
    <p><strong>Domain and Path</strong><br>
    The cookie domain and path define the scope of the
    cookie—they tell the browser that cookies should only be sent back to
    the server for the given domain and path. <strong>If not specified, they
    default to the domain and path of the object that was requested.</strong></p>
  </blockquote>
  
  <p>So if we push down:</p>
  
  <pre><code>Response.Cookies.Add(new HttpCookie("Banana", "2")
  {
  
  });
  </code></pre>
  
  <p>We should get a cookie with the domain used being the domain from the requested object, in this case it should be "testdomain.com".</p>
  
  <h2>W3</h2>
  
  <p>W3 states in the specification for cookies: <a href="http://www.w3.org/Protocols/rfc2109/rfc2109" rel="noreferrer">http://www.w3.org/Protocols/rfc2109/rfc2109</a></p>
  
  <blockquote>
    <p>Domain=domain</p>
    
    <p>Optional.  The Domain attribute specifies the domain for which the
    cookie is valid.  <strong>An explicitly specified domain must always start
    with a dot.</strong></p>
  </blockquote>
  
  <p>So if we push down:</p>
  
  <pre><code>Response.Cookies.Add(new HttpCookie("Banana", "1")
  {
      Domain = Request.Url.Host
  });
  </code></pre>
  
  <p>We pushed down the host-name explicitly, we should get a domain name set on the cookie which would be prefixed with the dot, in this case it should be ".testdomain.com".</p>
  
  <p>It also states what's on Wikipedia:</p>
  
  <blockquote>
    <p>Domain Defaults to the request-host.  (Note that there is no dot at
    the beginning of request-host.)</p>
  </blockquote>
  
  <hr>
  
  <p>With me so far?</p>
  
  <p>If I use the first method, defining a Domain:</p>
  
  <pre><code>Response.Cookies.Add(new HttpCookie("Banana", "1")
  {
      Domain = Request.Url.Host
  });
  </code></pre>
  
  <p>This is the results:</p>
  
  <p>IE9: 1 cookie</p>
  
  <p><img src="https://i.stack.imgur.com/SDPYZ.png" alt="IE with 1 cookie and domain explicitly set"></p>
  
  <p>Opera: 1 cookie</p>
  
  <p><img src="https://i.stack.imgur.com/MZ5sY.png" alt="Opera with 1 cookie and domain explicitly set"></p>
  
  <p>Firefox: 1 cookie</p>
  
  <p><img src="https://i.stack.imgur.com/o8ZCE.png" alt="Firefox with 1 cookie and domain explicitly set"></p>
  
  <p>Chrome: 1 cookie</p>
  
  <p><img src="https://i.stack.imgur.com/cgSWj.png" alt="Chrome with 1 cookie and domain explicitly set"></p>
  
  <p>As you can see, both Opera and IE both set an EXPLICIT domain without the dot prefix.</p>
  
  <p>Both Firefox and Chrome DO set the EXPLICIT domain with a dot prefix.</p>
  
  <p>If I use the following code:</p>
  
  <pre><code>Response.Cookies.Add(new HttpCookie("Banana", "2")
  {
  
  });
  </code></pre>
  
  <p>IE / Opera: Both have the exact same result, the domain WITHOUT the dot prefix.</p>
  
  <p>Funnily enough, Firefox and Chrome both create cookies WITHOUT the dot prefix.</p>
  
  <p>(I cleared all cookies and ran the code again)</p>
  
  <p>Firefox:</p>
  
  <p><img src="https://i.stack.imgur.com/3Tgkd.png" alt="Firefox with 1 cookie and domain explicitly set"></p>
  
  <p>Chrome: </p>
  
  <p><img src="https://i.stack.imgur.com/OngsB.png" alt="Chrome with 1 cookie and domain explicitly set"></p>
  
  <h2>INTERESTING BIT</h2>
  
  <p>This is where it gets interesting. If I write the cookies one after another like so:</p>
  
  <pre><code>Response.Cookies.Add(new HttpCookie("Banana", "1")
  {
      Domain = Request.Url.Host
  });
  Response.Cookies.Add(new HttpCookie("Banana", "2")
  {
  
  });
  </code></pre>
  
  <p>PERSONALLY I would expect one cookie to exist in the browser, because I assume it's based on the cookie name.</p>
  
  <p>Here's what i've observed:</p>
  
  <p>In IE / Opera, the LAST cookie set is the cookie that is used. This is because the Cookie name and Domain name are identical. </p>
  
  <p>If you explicitly define a domain name with a dot, both browser will still see 1 cookie, the last cookie of the same name.</p>
  
  <p>Chrome and Firefox on the other hand, see more than 1 cookie:</p>
  
  <p>I wrote the following JavaScript to dump the values to the page: </p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  (function () {
      var cookies = document.cookie.split(';');
      var output = "";
  
      for (var i = 0; i &lt; cookies.length; i++) {
          output += "&lt;li&gt;Name " + cookies[i].split('=')[0];
          output += " - Value " + cookies[i].split('=')[1] + "&lt;/li&gt;";
      }
  
      document.write("&lt;ul&gt;" + output + "&lt;/ul&gt;");
  })();
  
  &lt;/script&gt;
  </code></pre>
  
  <p>These are the results:</p>
  
  <p>IE - 2 cookies set (browser sees 1):</p>
  
  <p><img src="https://i.stack.imgur.com/bDFVl.png" alt="IE - 2 cookies set, the outcome"></p>
  
  <p>Opera - 2 cookies set (browser sees 1):</p>
  
  <p><img src="https://i.stack.imgur.com/Sl0r5.png" alt="enter image description here"></p>
  
  <p>Firefox - 2 cookies set and browser sees 2!:</p>
  
  <p><img src="https://i.stack.imgur.com/YvIe1.png" alt="enter image description here"></p>
  
  <p>Chrome - 2 cookies set and browser sees 2!:</p>
  
  <p><img src="https://i.stack.imgur.com/sWFf9.png" alt="enter image description here"></p>
  
  <hr>
  
  <p>Now you're probably wondering wtf all this is.</p>
  
  <p>Well: </p>
  
  <ol>
  <li>When you access the cookie by Name in C#, it gives you 1 cookie. (the first cookie that has that name)</li>
  <li>The browser sends ALL cookies to the server</li>
  <li>The browser doesn't send any information other than the key/value of the cookie. (this means the server doesn't care about the domain)</li>
  <li>You can access both cookies of the same name, if you retrieve them by index</li>
  </ol>
  
  <p>The problem...</p>
  
  <p>We had to change our Authentication to specify the domain in the cookie when we pushed it down.</p>
  
  <p>This broke Chrome and Firefox, users were no longer able to login, because the server would try authenticate the old auth cookie. This is because (from my understanding) it uses the Authentication Cookie Name to retrieve the cookie. </p>
  
  <p>Even tho there are two cookies, the first one is retrieved which happens to be the old one, authentication fails, user isn't logged in. SOMETIMES the correct cookie is first in the list, and the authentication succeeds...</p>
  
  <p>Initially we solved this by pushing a cookie with the old domain to expire it. This worked in Chrome and Firefox.</p>
  
  <p>But it now broke IE/Opera since both browsers don't care about the domain and only compare the cookie based on the name.</p>
  
  <p>My conclusion is that the domain on a cookie is a complete utter waste of time. </p>
  
  <p>Assuming that we must specify the domain, and we can't rely on users to clear their browser cache. How can we resolve this problem?</p>
  
  <h2>Update:</h2>
  
  <p>Digging into how .NET signs a user out. </p>
  
  <pre><code>if (FormsAuthentication._CookieDomain != null)
  {
      httpCookie.Domain = FormsAuthentication._CookieDomain;
  }
  </code></pre>
  
  <p>It looks like it's entirely possible for the Forms authentication to push an expired Auth cookie, that is entirely unrelated to the cookie the user is authenticated with. It doesn't use the current Auth Cookie's domain. </p>
  
  <p>Which it can't use anyway, since the domain isn't pushed back to the server with the cookie.  </p>
  
  <h2>Update 2</h2>
  
  <p>It seems FormsAuthentication is really broken. If you use an explicit domain name on a cookie when you authenticate the user, wait for the session to timeout, then refresh the page, the method of generating the cookie used by FormsAuthentication results in the domain being null which causes the browser to assign a dotless domain.</p>
  
  <p>It requires that Forms be assigned a domain up front for it to be assigned to the cookie, this breaks a multi-tenant system...</p>