Leroy Meijer
Leroy Meijer

Reputation: 1197

Request.Cookies is empty

I have been struggeling with a strange problem, if I may say so.

I have a controller in which I set a cookie, via this code:

var cookie = new HttpCookie("CookieName")
            {
                Value = Guid.NewGuid().ToString(),
                Expires = DateTime.Now.AddHours(12),
                Secure = false,
            };

            Response.Cookies.Add(cookie);

This cookie is being set correctly (I guess), because I see this cookie in my Chrome and also in FireFox in the console.

I also have a piece of code that checks whether or not the specific cookie is set:

    var token = Request.Cookies.Get("CookieName");
    if (token == null)
    {
       //Create a new cookie
    }

Also tried this:

var token = Request.Cookies["CookieName"]?.Value;

But nothing, when I check with my debugger I see that the Request.Cookies contains nothing! I just don't understand this.

I use VS2015 Community and I have my project set up to be started via IIS10, developing on WIndows 10 Pro - 64-bits I stopped using IIS Express because I thought the problem was being caused by the fact the url was like http://localhost:2322, I now work on a url which I let to 127.0.0.1 via my Host file on windows. domainA.local for example.

Added my web.config:

<compilation debug="true" targetFramework="4.6"/>
<httpRuntime targetFramework="4.5.1"/>
<httpCookies domain="" httpOnlyCookies="true"/>
<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="2880"/>
</authentication>

Also tried this while authentication was commented


Update 26-02-2016: I will try to explain my question in a different way.

I have a VS2015 project, an ASP.NET MVC C# application. This application should be listening to multiple URL's. Let's say it should listen to www.domainA.com and www.thisdomainiscool.com. Both domains are put in windows host file to 127.0.0.1. Because I run IIS on port 80 they both redirect to my project.

In my code I check via Request.Cookies["cookieName"] if the cookie exists. I also check the Url in Request and see that it is one of the specified URL's. According to each url I load a different style. I check this by httpContext.Request.Url.Host BUT although the URL is given, the cookies are not. Anytime the cookies are empty but I see them in my console of Chrome, but not in Fiddler. How can I make this work? In Fiddler I only see localhost:54121. Do I need to assume that this is the correct address? If I navigate to this URL I get a 404.

I hope this explanation, makes a lot of sense, thanks in advance.

Upvotes: 3

Views: 8851

Answers (2)

Hogan
Hogan

Reputation: 70513

The problem is cookies are typically associated with host names not IP address.

When you add an item to your hosts files you are allowing IP stack to resolve IP addresses to host names.

When you set the cookie to a host name the browser resolves that host name and also treats that location in respect to cookies.

The web server (in this case Visual Studio) does not need to do this -- it is getting requests and it does not know what it's host name is.

In IIs there are settings you make in the configuration files to fix this - I've not used VS studio in a while so there may be another trick but the easy way to solve it is to set the cookie on the host that visual studio believes is the host. You can see that in fiddler the response message.

so -- localhost or 127.0.0.1

(While 127.0.0.1 is an ip address, you can set a cookie to a full 4 digit ip address because it does not cause a security issue.)

There really is no other way to solve this problem.


The best way to solve problems with cookies is to use fiddler. Fiddler is a utility that was written years ago and has always been free. Telerik purchased it but you can still get it for free at http://www.telerik.com/fiddler

With fiddler you can see the traffic to and from your browser and look at the cookies being sent from both sides -- you can also change them to perform tests.

I expect about 10 mins after installing fiddler you will know what the problem is -- this will be much more productive than all of us trying to guess.

Upvotes: 3

JanMer
JanMer

Reputation: 1266

This works for me,

var cookie = new HttpCookie("CookieName")
        {
            Value = Guid.NewGuid().ToString(),
            Expires = DateTime.Now.AddHours(12),
            Secure = false,
        };

        Response.Cookies.Add(cookie);

to add the cookie

if (Request.Cookies["CookieName"] == null)
{
    var cookie = new HttpCookie("CookieName");
    cookie.Value = Guid.NewGuid().ToString();
    cookie.Expires = DateTime.Now.AddHours(12);
    cookie.Secure = false;
    Response.Cookies.Add(cookie);
}

Upvotes: 1

Related Questions