Tuesday, December 20, 2011

Check Session for expiry in Gloab.asax

One way to check, if session is expired or not is checking for cookies created in current context when session was created. We can check this in Session_Start function in Global.asax file.

E.g.

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
HttpContext context = HttpContext.Current;
HttpCookieCollection cookies = context.Request.Cookies;

if (cookies["starttime"] == null)
{
HttpCookie kookie = new HttpCookie("starttime", DateTime.Now.ToString());
kookie.Path = "/";
context.Response.Cookies.Add(kookie);
}
else
{
FormsAuthentication.SignOut();
context.Response.Redirect("Expired.aspx", false);
}
}

No comments:

Post a Comment