How to delete a cookie correctly when logging?

2

I have a series of cookies and sessions which are created at the time of login in a successful way in an application. My question is how can I eliminate these cookies and session created at the time of logout?

I have the following in a controller:

CookieService.SetCookie(model.mipropiedad, "cookie1"); // se encarga de crear una cookie de nombre cookie1 con el dato que le paso desde mi modelo.
CookieService.SetCookie(model.mipropiedad2, "cookie2");

Session["MiSession"] = model.mipropiedad3

Will it be correct to delete these cookies in the following way?

public static void LogOut()
{
    var mivar = new HttpCookie("cookie1");
    CookieSession.Expires = DateTime.Now.AddDays(-1);
    CookieSession.Value = string.Empty;
    HttpContext.Current.Response.Cookies.Add(mivar);
}

If this is the case, only 1 cookie with the name cookie1 would be deleting, but how do I delete the others together with the session?

    
asked by vcasas 14.06.2018 в 23:01
source

1 answer

2

to delete the cookies and what I would do is to recover them and update them as you do and finally apply a Session.Abandon() . Try this

for (int i=0; i<Request.Cookies.Count; i++)
{
    var cookie = new HttpCookie(Request.Cookies[i].Name);
    cookie.Expires = DateTime.Now.AddDays(-1);
    cookie.Value = string.Empty;
    Response.Cookies.Add(cookie);
}
Session.Abandon();
    
answered by 14.06.2018 / 23:46
source