45

Okay, this is really kinda starting to bug me. I have a simple Web project setup located at: "C:\Projects\MyTestProject\". In IIS on my machine, I have mapped a virtual directory to this location so I can run my sites locally (I understand I can run it from Visual Studio, I like this method better). I have named this virtual directory "mtp" and I access it via http://localhost/mtp/index.aspx. All this is working fine.

However, whenever I try to create a cookie, it simply never gets written out? I've tried this in FF3 and IE7 and it just plain won't write the cookie out. I don't get it. I do have "127.0.0.1 localhost" in my hosts file, I can't really think of anything else I can do. Thanks for any advice.

James

James McConnell
  • 2,060
  • 3
  • 18
  • 29

3 Answers3

94

The cookie specs require two names and a dot between, so your cookiedomain cannot be "localhost". Here's how I solved it:

  1. Add this to your %WINDIR%\System32\drivers\etc\hosts file: 127.0.0.1 dev.livesite.com

  2. When developing you use http://dev.livesite.com instead of http://localhost

  3. Use ".livesite.com" as cookiedomain (with a dot in the beginning) when creating the cookie. Modern browsers doesn't require a leading dot anymore, but you may want to use anyway for backwards compability.

  4. Now it works on all sites:

Sire
  • 3,620
  • 3
  • 33
  • 67
16

Since an answer has never been chosen, I suppose I can still throw something else out there.

One reason you can run into no cookies being written with an application running under localhost is the httpCookies setting in the web.config. If the domain attribute was set to a specific domain and you run under localhost, the cookies did not get written for me.

Remove the domain attribute in development and the cookies are written:

<!-- Development -->
<httpCookies httpOnlyCookies="true" requireSSL="false" />
<!-- Production -->
<!--<httpCookies domain=".domain.com" httpOnlyCookies="true" requireSSL="true" />-->
Jason Eades
  • 1,015
  • 3
  • 12
  • 26
  • great, saved me few hours of research. thought something is with my code. thanks son – sensei Oct 25 '17 at 08:45
  • This seems like a dangerous practice. Easy to forget/miss when publishing. – user158443 Nov 15 '19 at 15:01
  • 1
    @user158443 You wouldn't have these lines in the same config. The xml I posted was to illustrate a possible cause of the problem not a recommendation on what your config file should look like. At the very least you would handle this with config transformations or similar. – Jason Eades Dec 19 '19 at 22:02
  • Setting the requireSSL to "false" worked for me. – sd4ksb Nov 30 '20 at 17:50
0

Are you assigning an expiration date to the cookie? By default, the cookie will expire when the browser session expires, meaning it won't write anything to disk.

Robert C. Barth
  • 20,631
  • 6
  • 43
  • 52