How to Redirect to Another Page when Session Timeout in ASP.NET (when pages contain Ajax UpdatePanel)

A small solution to deal with session timeout when using Ajax UpdatePanel

Introduction

Session timeout in ASP.NET is such an important problem with web developer. When session ends, we run into some exceptional situations. There are also many solutions to deal with this problem on the Internet. But few of them have discussed about redirecting to another page when there is session timeout, especially when we work with Ajax UpdatePanel.

So, I suggest a way to do this.

Solution

My main idea is: 3 minutes before session is gone, we will alert the user to save his/her data or make any postbacks. If user still does not do anything, then 5 milliseconds before session goes, we will redirect the page to Login.aspx (or just refresh page by redirecting to itself).

Comments and Discussions

Thanks a lot. It works perfectly. I tried different techniques but none of them worked. One of the techniques worked but had problem with ajax UpdatePanel calls as it did not refresh the timeout.

Your solution is simple and efficient. In my case, as I have no master page in my application, so I defined it as a static method within a class in a namespace and passed the page as parameter to it. In my application I just needed to call it in two page_load, one in the default page and the other in the main container page which is the main page of the other controls and ASPX pages.

But what happend when you use modal popup windows. I test it and it fails because for each page the scriptmanager register the code. So you have 2 functions running the parent window and the popup window.

I have seen so many articles in the about redirecting to another page after timeout. In my opinion this article is the better, for its simplicity.I adapted the code to VB.NET and inserted the procedure in the web site master page code behind.

in my application - when I traverse for sometime to various tabs - suddenly login,aspx page is displayed.When I checked logs - but no debugger statement printed there..and no javascript alerts are seen.

So I verified web.config for timeout properties but couldnot find anything to be changed

I have a query regarding redirection to Login page, which I am not being able to trace out.

When I click on any of the tab in my application (which internally loads a new aspx page) sometimes itis redirected to Login.aspx, which is unexpected. Couldnot debug why this is happening even by javascript alerts and C# debugging statements.

Below is the view source piece from Login.aspx, just for clue<form name="Form1" method="post" action="login.aspx?ReturnUrl=%2fValuations%2fToDoList.aspx" id="Form1">

Valuations is the dll of application and ToDoList.aspx is the tab page I clicked which should load, but suddenly Login.aspx page is displayed. I traced the complete solution project.

I'm sure this is pretty easy to do but I'm javascript handicapped How do we go about adding a function to the alert box so that when they click the 'Ok' button, it will auto refresh their session state for them. This way the user won't have to know that they need to click on something that does a post back to restart their session timer?

Also, I don't suppose it's possible to add a real-time timer count down to that alert box is it so the user knows exactly how much time remains? This way if the user wasn't at the screen when the box first appeared but sat down soon after, they would know exactly how much time was really left?

If you want the page is refreshed after the user click 'OK' button, you just modify the javascript about like this: window.location.href = window.location.href (instead of window.location.href='login.aspx').

About the count-dount timer within the message box, you can search over the Internet to get the javascript code and modify it for your scenario. I think it is pretty easy

I did notice one strange thing though. Not sure how or why, but I noticed that if I left that 3 minute count down window open and I waited an easy 5 minutes before pressing "OK", instead of it already timing out (since there was only 3 minutes left), my session wasn't timed out at all... I did a post back to check and sure enough, the post back worked... any idea why this would be? since it's client side script, i can't see how the server would've gotten a postback to reset the timer... (i had the web.config session timeout set at 4 minutes for testing purposes).

Oh, well if that's the case than that explains why it didn't time out. cool, thanks for the help and the code! Now maybe the users will stop griping about wanting a warning of a pending session expiration... hehe

I am having the same problem. When the message pops up saying they will be logged off in three minutes, I don't hit the ok button (since this will be the most likely scenario in my project). I would assume that after 3 minutes, it would close that window and redirect to the login page. However, it doesn't do anything. Then, when I click the ok button, it times me out as it should.

The alert box is a modal dialog in the browser so it could well be that the thread that the timer is running in is suspended while the alert is being displayed. I'm not sure how browsers treat timers running in the main context, or how the threading model works internally. A better solution would be to create a div that is hidden and show it when the time runs out.

Hi,I implemented your code and it works fine but I got stuck with this problem:Because I am registering this function with setTimeOut of reminder session time, I get the confirm box eventhough I am working on it. Suppose I have a timeout of 20 min. I am registering this code with Session.Timeout delay.Even when I am working on the application, this function gets fired and I get the confirm box.

We are using sqlstate for storing sessions.

How can I avoid this confirm box to appear and make it appear when user has only 1 min left in the session?

You are working, but maybe you do not make any postbacks to server, so it still show message to confirm by the time session timeout.During your session, if you make some postbacks, the message must NOT appear (because the time to remind has been reset!).

The problem is I have an application with single page and multiple tabs. I put this script register block in Page_load of that page. It doesnt get called even if a new tab is created or a postback happens in the tab.

Hi, I think your tab control is not in the update panel. If you do not use update panel, you should try to replace ScriptManager.RegisterClientScriptBlock(...) with Page.RegisterClientScriptBlock(...) method

i just want to ask what could be wrong with my implementation because nothing is happening when i did something inside the update panel. the injected javascript code seem like its not taking in effect. I hope you can help me on this.

i registered the javascript by user ScriptManager.RegisterClientScriptBlock

if i run the application and do nothing, after the set timeout, it works fine, it redirects to the page it set. but when i did something on the update panel, say, i populated a gridview, and do nothing after that, wait for the set timeout, nothing happened, as if the javascript code didn't took in effect. please help me on this.

Did you add the function generate javascript to PageLoad (without inside !Page.IsPostBack) yet? After gridview changed, it would call this function to re-generate again the javascript snippet. And it should work fine.

Hi, I am using rad ajax now i am having the same problem i.e when i do a operation in the grid and leave the operation the page does nort redirect on session timeout.I have take care of adding generate javascript functon to PageLoad?please help

If you don't use alert in doReminder()-function, but a hidden html-control (like span) you can include a link in the control to refresh the session (like do a postback, form.submit or load something).

Or if you have a javascript in your input-fields:After doReminder()-time has passed the input-field javascript could set a flag that user have done some input near session expire time. Then you know that user is near and you can refresh the session just before it expires. A kinda like gmail saving the new message to draft.

If you don't use Ajax UpdatePanel in the page then the META tag refresh works great. But if the page contains UpdatePanel, it gets a problem. When the user made the postback, the META tag content does not refresh to get new time out for session (because META tag isn't inside any updatepanel). So it does not work correctly!

Hi tkrafael_net,1. About your inheritance method in order to valid session:Remember that, sometimes we ourself set Session["SessionName1"] == null although the current session state still alive. In this case, Session is null but the user's session state has not ended yet. So that, assume that we have 2 session variables, and we set session1=null, session2='abc' then if you check session is null then redirect to Login page, that's such not correct, because session2 still remain its value. Right?

2. Forms Authentication sometimes does not work correctly in some cases, I experienced...

The only major issue with trying the redirect approach (which many have tried) is that unless it's a "Full Page" postback (rather than an Ajax Callback), you are not allowed to execute a Response.Redirect.

This code will throw an exception if you are doing an Ajax callback which defeats the point of the article.