Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I just make a Login page with session object. Storing some values in session (userId, userName, UserRoleId). As I know that default time of session is 20 mins. I want that if user watching a page and as session expiry happen then redirect him to login page. How it possible is there any event exists which fire automatically as specific session expire? so that i write logic there.
I search about global.asax then i use this but it is not working

C#
void Session_End(object sender, EventArgs e)
  {
      Response.Redirect("login.aspx");
      // Code that runs when a session ends.
      // Note: The Session_End event is raised only when the sessionstate mode
      // is set to InProc in the Web.config file. If session mode is set to StateServer
      // or SQLServer, the event is not raised.

  }
Posted
Updated 2-May-15 8:27am
v3

You can't do this reliably. You can only redirect in response to a request and as session_end runs independently of a browser request you can't use it to redirect the client. Basically you can't force the browser to navigate to a page via a server-event, only from a page request.

To do this at all you'll need to use a javascript timer and that is unreliable too, especially if the user has two browser tabs open at once with one keeping the session going and the other idle. Throw into this issues where the session expires for reasons other than a timeout, and you might as well just not bother trying to do this. You can tell when a session is a new session and take them to a page to tell them their old session expired but that's about the best you can do. I just wouldn't bother.
 
Share this answer
 
Comments
Muhamad Faizan Khan 3-May-15 2:19am    
so what does gloabl.asax event means? how can i do this
F-ES Sitecore 3-May-15 6:59am    
The event in the global.asax file is ran by IIS when it expires a session, but the event runs "in the background" as it were, there is no request or response so you can't get data from the browser or push data to the browser on that event. It is for cleaning up any resources you might have to like delete temporary files or database entries. However it is notoriously unreliable, it doesn't always fire, and it doesn't fire for "out of process" sessions like those stored in SQL or state server.
Muhamad Faizan Khan 3-May-15 9:35am    
so what the solution
F-ES Sitecore 3-May-15 13:04pm    
There is none, what you're wanting to do isn't technically possible, so just don't bother doing it :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900