47

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

4
  • What are you using to check for the existence of the cookie? Commented Dec 23, 2008 at 22:17
  • Could you provide a sample code for the cookie creation part? Commented Dec 23, 2008 at 22:18
  • Sample code is really required here. Commented Dec 23, 2008 at 22:22
  • Possible duplicate of stackoverflow.com/questions/1134290/… Commented Jan 9, 2015 at 23:30

3 Answers 3

95

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:

Sign up to request clarification or add additional context in comments.

8 Comments

Awesome post, why is this not selected as the answer?
This is definitely the answer.
This is very useful information, as I was wondering why cookies I'd made were being deleted between debugging sessions
Then how does ASP.NET session work for localhost? Clearly it's storing cookies for the session. I can see them in my cookie inspector. Is the trick to set the path to "/"?
Bad Request - Invalid Hostname
|
18

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" />-->

4 Comments

great, saved me few hours of research. thought something is with my code. thanks son
This seems like a dangerous practice. Easy to forget/miss when publishing.
@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.
Setting the requireSSL to "false" worked for me.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.