Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't asp.net create cookies in localhost?

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

like image 877
James McConnell Avatar asked Dec 23 '08 22:12

James McConnell


People also ask

Why is cookie not being set?

Check out the OPTIONS response header ACCESS-CONTROL-ALLOW-CREDENTIAL whether it is set to true . If the server doesn't allow credentials being sent along, the browser will just not attach cookies and authorization headers. So this could be another reason why the cookies are missing in the POST cross-site request.

Does cookies work on localhost?

Secure cookies are set only on HTTPS, but not on http://localhost for all browsers. And because SameSite:none and __Host also require the cookie to be Secure , setting such cookies on your local development site requires HTTPS as well.

Does ASP Net session use cookies?

Yes, by default ASP.NET Session use cookies.

How do I access cookies in asp net?

You may use Request. Cookies collection to read the cookies. Show activity on this post. HttpContext.


2 Answers

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:

    • http://dev.livesite.com
    • http://www.livesite.com
    • http://livesite.com
like image 194
Sire Avatar answered Oct 29 '22 20:10

Sire


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" />--> 
like image 26
Jason Eades Avatar answered Oct 29 '22 18:10

Jason Eades