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.