1

I have tried other SO questions and none have helped. basically they say I need to set the expiration to a negative value (which I already did), and then add it.

Here's my problem though. I am trying to delete a cookie that already exists.

code

    public ActionResult LogOff()
    {
        Response.Cookies["user"].Expires = DateTime.Now.AddDays(-1D);
        Response.Cookies.Add(Response.Cookies["user"]);
        return RedirectToAction("Index", "Home");
    }

I have tried a few things but basically if I create a new cookie, that one gets deleted but not the original user cookie. Basically I am trying to grab the cookie named user, and clear it. What I have is not working so I tried to make a temp variable of cookie and delete that (saw this on another SO question, didn't make sense that it would work in my case but I tried anyways):

    public ActionResult LogOff()
    {
        //AuthenticationManager.SignOut();
        HttpCookie temp = Response.Cookies["user"];
        temp.Expires = DateTime.Now.AddDays(-1D);
        Response.Cookies.Add(temp);
        return RedirectToAction("Index", "Home");
    }

This did not work either for some reason. Although I kind of get it, all I'm doing is saying "new http cookie = cookie named user, delete the new cookie" but I'm still where I started.

Robert full code I am using

public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };
            InsertToDB(model.Email, model.Password);
            HttpCookie cookie = new HttpCookie("user");
            String loginCred = model.Email.Trim();
            cookie.Value = loginCred;
            cookie.Expires = DateTime.Now.AddSeconds(180);
            Response.Cookies.Add(cookie);
            return RedirectToAction("Index", "Home");

        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

I don't have it set up for login yet, but the register is what is setting the cookie for now.

Then when you click logoff:

    public ActionResult LogOff()
    {
        HttpCookie temp = Response.Cookies["user"];
        temp.Expires = DateTime.Now.AddDays(-1D);
        Response.Cookies.Add(temp);


        return RedirectToAction("Index", "Home");
    }

And I totally understand this isn't the safest way to handle this. I know. This isn't the final product I just need something that works for now.

Jordan M
  • 1
  • 2
  • 13
  • Well because you have to? At least from what I understand. I thought it was kind of like a 'ExecuteNonQuery'. I tried without the Add, I even tried 'Set' which also did not work. – Jordan M Nov 11 '14 at 18:54
  • Follow [this example precisely](http://stackoverflow.com/a/5122611/102937), and let us know what happens. Note that the example appears to be *creating a new cookie with the same name,* not trying to retrieve the existing one. Yes, it's counter-intuitive. – Robert Harvey Nov 11 '14 at 18:54
  • Nope, just as I thought this did not work either. And I think I know why. We are simply creating a new cookie and deleting it, but we're not doing anything to the cookie that is already there. – Jordan M Nov 11 '14 at 18:56
  • Show me the exact code you're using. – Robert Harvey Nov 11 '14 at 18:58
  • edited the OP with full code. – Jordan M Nov 11 '14 at 19:04
  • See the answer I've posted below. – Robert Harvey Nov 11 '14 at 19:05

1 Answers1

2

Your code should look exactly like this (based on the example I linked):

public ActionResult LogOff()
{
    var temp = new HttpCookie("user");
    temp.Expires = DateTime.Now.AddDays(-1);
    Response.Cookies.Add(temp);
    return RedirectToAction("Index", "Home");
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • yeah i have done this before and it worked before but its not working for this. i can't imagine this being a result of me working locally can it? anyways thanks for the try. – Jordan M Nov 11 '14 at 19:08
  • When you say it doesn't work, what do you mean exactly? Also, did you actually plug this code in and test it, or did you just assume it doesn't work? – Robert Harvey Nov 11 '14 at 19:09
  • Yes I did plug in and test. – Jordan M Nov 11 '14 at 19:11
  • Then the problem must be elsewhere, unless you can prove to yourself in some other way that the cookie is not actually getting reset to expire yesterday. That should be easy enough to test. – Robert Harvey Nov 11 '14 at 19:14
  • yeah i figured out the method isn't even being called somehow. thats why i checked your answer, =/ it didn't make any sense that it wouldn't call the function so i never thought of it until i saw that it wasn't. – Jordan M Nov 11 '14 at 19:26