Here is an example:
protected void Session_Start(Object sender, EventArgs e)
{
//Check if there is a cookie from when the session started.
//If not, the session has not been started yet,
//so add the cookie.
HttpContext context = HttpContext.Current;
HttpCookieCollection cookies = context.Request.Cookies;
HttpCookie SessionCookie = cookies["SessionStarted"];
if (SessionCookie == null)
{
HttpCookie cookie = new HttpCookie("SessionStarted", DateTime.Now.ToString());
cookie.Path = "/";
context.Response.Cookies.Add(cookie);
}
else
{
//If there was a cookie, but we are in this method starting a session
//Then the session must have expired or been reset.
//TODO: Do something useful with this info, log it, etc.
//update the cookie with the new session start time
cookies.Remove("SessionStarted");
SessionCookie.Value = DateTime.Now.ToString();
context.Response.Cookies.Add(SessionCookie);
//Redirect to the whatever page you want to handle this situation.
//An error page, login page, etc.
context.Response.Redirect("~/Login.aspx", true);
}
}
---thnx...enjoy programming.
No comments:
Post a Comment