I want to make all sessions null in one statement when i make logout.Can anyone tell me how to make all session null.Currently i am making Sessions null as follows:
Session["UserID"]=null;
Session["UserName"]=null;
Session["Photo"]=null;
I want to make all sessions null in one statement when i make logout.Can anyone tell me how to make all session null.Currently i am making Sessions null as follows:
Session["UserID"]=null;
Session["UserName"]=null;
Session["Photo"]=null;
This example will do the job:
Session["UserID"] = "test1 value";
Session["UserName"] = "test2 value";
Session["Photo"] = "test3 value";
foreach (string key in Session.Keys)
{
Session.Remove(key);
}
Response.Write(Session["UserID"]);
Response.Write(Session["UserName"]);
Response.Write(Session["Photo"]);
This could have been also done using linq, like here.
var sessionsToRemove = Session.Keys.Cast<string>().ToList();
foreach (var key in sessionsToRemove)
{
Session.Remove(key);
}
You can try this:
HttpSessionState session = HttpContext.Current.Session;
session.RemoveAll();