 |
|
 |
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.
Private Sub CheckSessionTimeout()
Dim msgSession As String = _
"Warning: in 3 minutes, if you don't do anything " + _
" our system will redirect you to the login page. Please, save all your unsaved data."
Dim int_MilliSecondsTimeReminder As Long = (Me.Session.Timeout * 60000) - 3 * 60000
Dim int_MilliSecondsTimeOut As Long = (Me.Session.Timeout * 60000) - 5
Dim str_Script As String = _
"var myTimeReminder, myTimeOut; " + _
"clearTimeout(myTimeReminder); " + _
"clearTimeout(myTimeOut); " + _
"var sessionTimeReminder = " + _
int_MilliSecondsTimeReminder.ToString() + "; " + _
"var sessionTimeout = " + int_MilliSecondsTimeOut.ToString() + ";" + _
"function doReminder(){ alert('" + msgSession + "'); }" + _
"function doRedirect(){ window.location.href=""sessionexpired.aspx""; }" + _
"myTimeReminder=setTimeout('doReminder()', sessionTimeReminder);" + _
"myTimeOut=setTimeout('doRedirect()', sessionTimeout); "
ScriptManager.RegisterClientScriptBlock(Me.Page, Me.GetType(), "CheckSessionOut", str_Script, True)
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
CheckSessionTimeout()
End Sub
Jacques Zetune
|
|
|
|
 |
|
 |
Have seen so many examples on the web, this was the simplest and better. Worked very nice to me.
|
|
|
|
 |
|
 |
Hi Nguyen
your code sample save my Web Application using the UpdatePanel.
Thanks!!
5*****
|
|
|
|
 |
|
 |
superb ur code is the coolest and simple to understand
|
|
|
|
 |
|
 |
Hi,
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
<authentication mode="Forms">
<forms loginUrl="login.aspx" protection="All" timeout="600" slidingExpiration="true" >
</forms>
</authentication>
and
<sessionState mode="InProc" timeout="600" />
Thanks and regards,
- Ajay Kale
|
|
|
|
 |
|
 |
Hi
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 it
is 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.
Can you please help me...?
- Ajay K
|
|
|
|
 |
|
 |
how to disable browser's back n forward button after logged out? or how to expire a page when logged out?
|
|
|
|
 |
|
 |
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?
|
|
|
|
 |
|
 |
Hi Robert,
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
Live free, die well!
http://www.sirvina.com
|
|
|
|
 |
|
 |
Thanks, I appreciate it
I'll give it a shot and see how it works.
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).
|
|
|
|
 |
|
 |
Ok. It is really a strange.
When you implement this, you already renew the session too
Live free, die well!
http://www.sirvina.com
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
What do you mean, renew the session too?
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.
Thanks in advance!
Rosemary
|
|
|
|
 |
|
 |
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.
I use the following code in my master page head:
<script language="javascript" type="text/javascript">
function doRefresh(){ javascript:__doPostBack(''); }//window.location.href=window.location.href; }
function doRedirect(){ window.location.href='login.aspx'; }
function getStyleObject(objectId) {
// checkW3C DOM, then MSIE 4, then NN 4.
if(document.getElementById && document.getElementById(objectId))
return document.getElementById(objectId).style;
else if (document.all && document.all(objectId))
return document.all(objectId).style;
else if (document.layers && document.layers[objectId])
return document.layers[objectId];
else
return false;
}
function setObjectVisibility(objectId, newVisibility) {
var styleObject = getStyleObject(objectId);
if (styleObject)
{
styleObject.visibility = newVisibility;
return true;
}
else
return false;
}
function showUserMessage()
{
setObjectVisibility("userMessage","visible");
}
function hideUserMessage()
{
setObjectVisibility("userMessage","hidden");
}
</script>
with a div that looks like this
<div id="userMessage" style="position: absolute; background-color: white; z-index: 1;
display: block; top: 5px; left: 20px; visibility:hidden; border: 1px solid black;">
<table width="400" style="width: 200px; height: auto;
background-color:white;">
<tr>
<td>
Warning: Within next 3 minutes, if you do not do anything, the system will redirect
to the login page. Please save changed data.
</td>
</tr>
<tr>
<td align="right"><asp:Button CssClass="groupButton" ID="btnStillHere" OnClientClick="doRefresh()" Text="OK" runat="server" /></td>
</tr>
</table>
</div>
and this in my code behind:
protected void Page_Load(object sender, EventArgs e)
{
CheckSessionTimeout();
}
private void CheckSessionTimeout()
{
int int_MilliSecondsTimeReminder = (Session.Timeout * 60000) - 3 * 60000;
int int_MilliSecondsTimeOut = (Session.Timeout * 60000) - 5;
StringBuilder sb = new StringBuilder();
sb.AppendLine("<script language="\"javascript\"" type="\"text/javascript\"">");
sb.AppendLine("var myTimeReminder, myTimeOut;");
sb.AppendLine("clearTimeout(myTimeReminder);");
sb.AppendLine("clearTimeout(myTimeOut);");
sb.AppendFormat("var sessionTimeReminder = {0};\n", int_MilliSecondsTimeReminder);
sb.AppendFormat("var sessionTimeout = {0};\n", int_MilliSecondsTimeOut);
sb.AppendLine("myTimeReminder=setTimeout('showUserMessage()', sessionTimeReminder);");
sb.AppendLine("myTimeOut=setTimeout('doRedirect()', sessionTimeout);");
sb.AppendLine("</script>");
Page.RegisterClientScriptBlock("CheckSessionOut", sb.ToString());
}
This works well for me
Life is a game. Play to win
|
|
|
|
 |
|
 |
Thanks Ksalvage very much.
Your code with div alert works very great!
Live free, die well!
http://www.sirvina.com
|
|
|
|
 |
|
 |
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?
Thanks in advance,
Chaggu.
|
|
|
|
 |
|
 |
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!).
Live free, die well!
http://www.sirvina.com
|
|
|
|
 |
|
 |
Thanks for your quick reply.
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.
Can you please suggest me something else, I would really appreciate?
Thanks,
Chaggu.
|
|
|
|
 |
|
 |
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
Live free, die well!
http://www.sirvina.com
|
|
|
|
 |
|
 |
I am using an Alert box instead of Confirm box and it works for my scenario.
Thanks for replying.
Thanks,
Chaggu.
|
|
|
|
 |
|
 |
It works fine and save time for me.
Thanks.
tnd
|
|
|
|
 |
|
 |
Thanks for this article.
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.
Thanks.
|
|
|
|
 |
|
 |
Could you post something about your code?
Did you try to register javascript with ScriptManager.RegisterClientScriptBlock(...) method instead of Page.RegisterClientScriptBlock(...)?
Live free, die well!
|
|
|
|
 |
|
 |
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.
Thanks
|
|
|
|
 |
|
 |
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.
Live free, die well!
http://www.sirvina.com
|
|
|
|
 |