In my website when the user clicks on the \"Logout\" button, the Logout.aspx page loads with code Session.Clear()
.
In ASP.NET/C#, does this clear all co
Try something like that:
if (Request.Cookies["userId"] != null)
{
Response.Cookies["userId"].Expires = DateTime.Now.AddDays(-1);
}
But it also makes sense to use
Session.Abandon();
besides in many scenarios.
No, Cookies can be cleaned only by setting the Expiry date for each of them.
if (Request.Cookies["UserSettings"] != null)
{
HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}
At the moment of Session.Clear()
:
Session
collection are removed. Session_End
event is not happen.If you use this method during logout, you should also use the Session.Abandon
method to
Session_End
event:
Response.Cookies["UserSettings"].Expires = DateTime.Now.AddDays(-1)
Unfortunately, for me, setting "Expires" did not always work. The cookie was unaffected.
This code did work for me:
HttpContext.Current.Session.Abandon();
HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
where "ASP.NET_SessionId"
is the name of the cookie. This does not really delete the cookie, but overrides it with a blank cookie, which was close enough for me.
Though this is an old thread, i thought if someone is still searching for solution in the future.
HttpCookie mycookie = new HttpCookie("aa");
mycookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(mycookie1);
Thats what did the trick for me.