0

I have a cookie which is set when a user accesses the page /auth/ of my MVC3 application.

When a user posts the form data back to the server I modify the cookie by changing the value it has assigned. I then use Response.Cookies.Set(mycookie); to change the cookie to the value of mycookie.

The issue I am having is that when the page is first loaded 'get' request the cookie appears as a cookie. Upon receiving the post response back the cookie now appears as a session with a completely different expiry date.

CODE::

[HttpGet]
public ActionResult Auth()
{
    var cookie = Request.Cookies.Get(login_cookie);
    if (cookie == null || string.IsNullOrEmpty(cookie.Value))
    {
        Response.Cookies.Add(new HttpCookie(login_cookie) { Expires = DateTime.Now.AddMinutes(5), Value = "0", HttpOnly = true,  });
    }
    .....

    return View();
}


[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(Login loginform)
{
    int attempts = 0;
    HttpCookie login_cookie_data = Request.Cookies.Get(login_cookie);

    ....

    Response.Cookies.Set(login_cookie_data);
    return View();
}

Resolved

Issues was with machine. I restart and it sorted all issues.

Matt Seymour
  • 8,880
  • 7
  • 60
  • 101
  • 1
    I got lost on "the cookie appears as a cookie". First, what do you call "postback"? (there is not such a thing in MVC). And what do you mean with "the cookie now appears a s a session" ? – vtortola Jan 31 '12 at 10:42
  • What cookie value are you changing? Can you post the code that you're using to do this? – markpsmith Jan 31 '12 at 11:42
  • @markpsmith I have added some code examples. – Matt Seymour Jan 31 '12 at 12:29
  • @vtortola, ermmm yeah there is such a thing, it is when you post back data to the server, hence a postback. – Matt Seymour Jan 31 '12 at 12:33
  • Agreed with vtorola. I didn't understand anything from this question. A full and yet concise example allowing to reproduce the problem would be helpful since the description is quite unclear (stripped down to a minimum Model(s), Controller(s), View(s)). – Darin Dimitrov Jan 31 '12 at 12:44
  • That code is enough to replicate the issue on my machine – Matt Seymour Jan 31 '12 at 13:03
  • @aspect, not on mine. I don't see any view. I don't know what the `Login` class is. There seem to be some `....` in your actions which are unlikely to compile. In your GET action you seem to be using some `login_cookie` variable which is totally unclear where is defined, etc... I think you get the point what I meant when I asked for a short but concise example allowing to reproduce the problem. So obviously in addition to providing such example it would be helpful to describe the exact workflow you are using to navigate through it. – Darin Dimitrov Jan 31 '12 at 13:06
  • @aspect - should login_cookie be "login_cookie"? – markpsmith Jan 31 '12 at 13:09
  • @aspect - also, vtortola is correct, in MVC there is no 'Postback' as we previously knew it in webforms, although I understand what you mean when you say the functionality still exists. There's a good explanation [here](http://stackoverflow.com/questions/4561719/asp-mvc-no-view-state-or-post-back) – markpsmith Jan 31 '12 at 13:13

1 Answers1

1

Add the root path when creating your cookie in the cookie constructor.

... new HttpCookie(login_cookie) { Path = "/", ...
Softlion
  • 12,281
  • 11
  • 58
  • 88