All the best. Can you send the Call Back Code to me since I am also looking for this type of support in my web page and I am weak in Javascript. My Mail ID is
kbnsarma@yahoo.com
It's an old thread and I just want to post the solution we added to our software.
1) In your code-behind, first add in the code to figure out the form authentication timeout value you added in web.config
2) Find out user's preference, i.e how many minutes before timeout, they want the warning? (We stored that in the user Profile object)
3) Have asp.net register the javascript with those values to the client side.
4) Client side script start counting based on those values and warn the users.
Codebehind
protected void Page_Load(object sender, EventArgs e)
{
this.keepAlive();
}
private int GetFormsTimeout()
{
System.Xml.XmlDocument x = new System.Xml.XmlDocument();
x.Load(Request.PhysicalApplicationPath + "/web.config");
System.Xml.XmlNode node = x.SelectSingleNode("/configuration/system.web/authentication/forms");
int Timeout = int.Parse(node.Attributes["timeout"].Value, System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
return Timeout;
}
private void keepAlive()
{
//figure out the full url of the logout page configured in web.config + the return url which is the current page
String logoutUrl = FilesUtil.LoginUrl + FormsAuthentication.LoginUrl+"?ReturnUrl="+Request.Url;
String minutesToWarning = "";
//figure out how many minutes before time out the user wants to get warned.
//if the user selected "-1", then it never gets timedout. If not, figure out, how many minutes after the page loaded, give the user the warning if (!minutesBeforeLoggedOut.Equals("-1")) { int formsTimeout = GetFormsTimeout(); minutesToWarning = Convert.ToString(formsTimeout - Convert.ToInt32(minutesBeforeLoggedOut)); } String script = @" <script type='text/javascript'> var minutesToWarning = " + minutesToWarning + @"; var minutesBeforeLoggedOut = "+ minutesBeforeLoggedOut + @"; var loginUrl = '" + logoutUrl+ @"'; </script> ";
if (!ClientScript.IsClientScriptBlockRegistered("WarnTimeOut")) ClientScript.RegisterClientScriptBlock(typeof(Page), "WarnTimeOut", script); }
Javascript used
<script type="text/javascript">
var secondsPassed = 0;
function ShowTimePassed() { minutesBeforeLoggedOut = parseInt(minutesBeforeLoggedOut); secondsPassed+=1;
if(minutesBeforeLoggedOut == -1) { if(secondsPassed == 30) { var img = new Image(1,1); img.src = 'KeepAlive.aspx?date='+escape(new Date()); secondsPassed = 0; } } else{ if(secondsPassed ==minutesToWarning*60) { var answer;
var currentTime = new Date(); var expiredTime = new Date(); var minutes = expiredTime.getMinutes(); minutes+=minutesBeforeLoggedOut; expiredTime.setMinutes(minutes);
if(minutesBeforeLoggedOut==1) answer = confirm("It is now "+ currentTime.toLocaleTimeString()+" You have "+minutesBeforeLoggedOut+" minute left before getting logged out. Do you want to extend the session?"); else answer = confirm("It is now "+ currentTime.toLocaleTimeString()+" You have "+minutesBeforeLoggedOut+" minutes left before getting logged out. Do you want to extend the session?"); if(answer){ var img = new Image(1,1); img.src = 'KeepAlive.aspx?date='+escape(new Date()); secondsPassed = 0; currentTime = new Date(); if(currentTime>expiredTime){ alert("You've exceeded the time needed to extend the session. You will be logged out now"); window.location = loginUrl; } } } } }
btw, just to clarify, after research, i realized the subject of the thread is wrong. I did not mean session time out, i meant forms authentication time out. They are two different things.
Liming Xu
Jumptree ASP.NET 2.0 Project Management, Bug Tracking & Task Management - Encourages collaboration, provides transparency and comes with metrics.
JQuery Gantt, JQuery Calendar, SiteMinder/AD Authentication, Customizable Languages, Custom Fields, Lucene.Net Search, and more
if (answer) {
var img = new Image(1, 1);
img.src = 'default.aspx?date=' + escape(new Date());
secondsPassed = 0;
currentTime = new Date();
if (currentTime > expiredTime) {
alert("You've exceeded the time needed to extend the session. You will be logged out now");
window.location = loginUrl;
}
}
forgot to mention, there is a blank "keepalive.aspx" in the directory. In the javascript code, after you clicked okay, an empty image object was costructed like so
var img = new Image(1,1); img.src = 'KeepAlive.aspx?date='+escape(new Date());
that will hit the keepalive.aspx page and such extend the user session.
As for the last piee of the code, users sometimes return to the window way after the warning period has expired. That was a measure to let the users know that, even if they clicked "okay", it's no use now, because they did not take any action during the warning period. ASP.NET by default already timed you out, there is nothing we can do now. Such, we redirect them back to the login page
Liming Xu
Jumptree ASP.NET 2.0 Project Management, Bug Tracking & Task Management - Encourages collaboration, provides transparency and comes with metrics.
JQuery Gantt, JQuery Calendar, SiteMinder/AD Authentication, Customizable Languages, Custom Fields, Lucene.Net Search, and more
The code which u gave is not working for me....plz if u have any othe solution for "2 minutes before session timeout, warn the user and extend it" this provide me.
shantanushuk...
Member
475 Points
152 Posts
Re: 2 minutes before session timeout, warn the user and extend it
Jan 23, 2008 08:59 AM|LINK
Perhaps this may be usefull for you....
http://www.common-controls.com/en/resources/docs/CC_Eventhandler_EN.pdf
CallBack Page Callback
http://www.linkedin.com/in/shantanushukla
KBNSarma
Member
192 Points
67 Posts
Re: 2 minutes before session timeout, warn the user and extend it
Jan 23, 2008 09:11 AM|LINK
Thank you Mr. Shantanu.
KBN Sarma
Liming
Contributor
5367 Points
1076 Posts
Re: 2 minutes before session timeout, warn the user and extend it
Apr 16, 2009 01:37 PM|LINK
It's an old thread and I just want to post the solution we added to our software.
1) In your code-behind, first add in the code to figure out the form authentication timeout value you added in web.config
2) Find out user's preference, i.e how many minutes before timeout, they want the warning? (We stored that in the user Profile object)
3) Have asp.net register the javascript with those values to the client side.
4) Client side script start counting based on those values and warn the users.
Codebehind
protected void Page_Load(object sender, EventArgs e) { this.keepAlive(); } private int GetFormsTimeout() { System.Xml.XmlDocument x = new System.Xml.XmlDocument(); x.Load(Request.PhysicalApplicationPath + "/web.config"); System.Xml.XmlNode node = x.SelectSingleNode("/configuration/system.web/authentication/forms"); int Timeout = int.Parse(node.Attributes["timeout"].Value, System.Globalization.CultureInfo.InvariantCulture.NumberFormat); return Timeout; } private void keepAlive() { //figure out the full url of the logout page configured in web.config + the return url which is the current page String logoutUrl = FilesUtil.LoginUrl + FormsAuthentication.LoginUrl+"?ReturnUrl="+Request.Url; String minutesToWarning = "";Javascript used
Jumptree ASP.NET 2.0 Project Management, Bug Tracking & Task Management - Encourages collaboration, provides transparency and comes with metrics.
JQuery Gantt, JQuery Calendar, SiteMinder/AD Authentication, Customizable Languages, Custom Fields, Lucene.Net Search, and more
Liming
Contributor
5367 Points
1076 Posts
Re: 2 minutes before session timeout, warn the user and extend it
Apr 16, 2009 01:38 PM|LINK
Jumptree ASP.NET 2.0 Project Management, Bug Tracking & Task Management - Encourages collaboration, provides transparency and comes with metrics.
JQuery Gantt, JQuery Calendar, SiteMinder/AD Authentication, Customizable Languages, Custom Fields, Lucene.Net Search, and more
jagjot
Contributor
2648 Points
1229 Posts
Re: 2 minutes before session timeout, warn the user and extend it
Jul 14, 2009 11:27 AM|LINK
Great code. works great for me.
But i dont understand when use will click OK (in timeout warning window). How it will refresh the session. i dont see page refreshing?
Thanks in advance.
M.Sc, MIEEE, MCP, CCNA
jagjot
Contributor
2648 Points
1229 Posts
Re: 2 minutes before session timeout, warn the user and extend it
Jul 14, 2009 02:33 PM|LINK
i dont understand what is the use of var img
if (answer) {
var img = new Image(1, 1);
img.src = 'default.aspx?date=' + escape(new Date());
secondsPassed = 0;
currentTime = new Date();
if (currentTime > expiredTime) {
alert("You've exceeded the time needed to extend the session. You will be logged out now");
window.location = loginUrl;
}
}
any ideas?
M.Sc, MIEEE, MCP, CCNA
Liming
Contributor
5367 Points
1076 Posts
Re: 2 minutes before session timeout, warn the user and extend it
Jul 17, 2009 03:14 PM|LINK
Hey jagjot,
forgot to mention, there is a blank "keepalive.aspx" in the directory. In the javascript code, after you clicked okay, an empty image object was costructed like so
Jumptree ASP.NET 2.0 Project Management, Bug Tracking & Task Management - Encourages collaboration, provides transparency and comes with metrics.
JQuery Gantt, JQuery Calendar, SiteMinder/AD Authentication, Customizable Languages, Custom Fields, Lucene.Net Search, and more
itsumapathyk
Participant
1550 Points
358 Posts
Re: 2 minutes before session timeout, warn the user and extend it
Jul 17, 2009 04:02 PM|LINK
Hope this helps,
msdn.microsoft.com/en-us/library/ms178208.aspx
www.developer.com/net/asp/article.php/3485991
www.codeproject.com/KB/aspnet/ClientCallBackAspNet2.aspx
www.CodeCollege.NET,www.interviewsguru.com,The Encyclopedia of Sites ,www.thekumbakonam.com
Pikesville P...
Participant
1036 Points
197 Posts
You're missing something
Dec 03, 2009 08:33 PM|LINK
Sliding forms Authentication only refreshes the auth ticket after the ticket is 1/2 way expired.
If a user has a 30 min ticket & refreshes their browser every min for 14 min, then does nothing for 16 minutes, they get logged out!
You need to test for when Auth ticket is set & how old it is. If it is older than 15 min and you refresh, it gets reset to 30 mins...
You haven't had a beer until you've learned how to make your own...
gorla.naveen
Member
24 Points
11 Posts
Re: 2 minutes before session timeout, warn the user and extend it
Mar 28, 2010 10:31 AM|LINK
Hi Liming
The code which u gave is not working for me....plz if u have any othe solution for "2 minutes before session timeout, warn the user and extend it" this provide me.