Question subject: how to write code message to user before session will expire
Posted: Tue Dec 30, 2008 2:45 am
Joined: Tue Dec 30, 2008 12:55 am Posts: 3 Has thanked: 0 time Have thanks: 0 time
Hi, Would you please give me exaple of how to code (web application - vb.net for 2003 and where ) time controls and to popup a message to user and redirect to login page when the session times out.. Thank you very much
msi_333
Question subject: Re: how to write code message to user before session will expire
Posted: Tue Dec 30, 2008 7:01 pm
Joined: Tue Mar 27, 2007 10:55 pm Posts: 2103 Location: Earth Has thanked: 39 time Have thanks: 57 time
i found this in some blog : Session.Timeout means that after how much time the user's session will expire and the user will not be able to access the items in the Session object. By default the Session.Timeout is 20 minutes. You can change this through the web.config or the page level code. Let's see a small example:
Code:
Session.Timeout = 1;
Session["Name"] = "Session.Timeout means that after how much time the user's session will expire and the user will not be able to access the items in the Session object. By default the Session.Timeout is 20 minutes. You can change this through the web.config or the page level code. Let's see a small example:
Session.Timeout = 1;
Session["Name"] = "Tomas andro";
In the above code I am setting the Session.Timeout to "1" minute. After that I put "Tomas andro" in the Session object. This means that if you don't make request to the server and sit idle then after 1 minute you won't be able to access the "Name" key in the Session bag. A good scenario will be a user filling out a long form and you store the values of the form in a Session object. The user fills only couple of fields and then sit idle for 25 minutes keeping in mind that the Session timeout is 20 minutes. Now, if the user tries to retrieve something from the Session object after 25 minutes it will throw the ArgumentNullException as the Session object has been removed.
The question is how do we renew the Session and how do we notify the user that his session is about to expire. The idea is to use JavaScript and notify the user 10 seconds before the Session is about to expire.
First, here is the code from the TestPage.aspx.cs:
public partial class TestPage : BasePage { protected void Page_Load(object sender, EventArgs e) {
protected void Button2_Click(object sender, EventArgs e) { if (Session["Name"] == null) throw new ArgumentNullException("Session[Name] is null"); } }
With the click of a button I insert my name "Tomas andro" into the Session object. The Session.Timeout is 1 minute and is configured in the web.config file.
Here is the code for the BasePage.cs:
public class BasePage : System.Web.UI.Page {
public BasePage() { this.Load += new EventHandler(BasePage_Load); }
When the page loads I register the page to use AJAXPRO.NET library and inject the timer script. The InjectTimerScript method injects the setInterval method which fires 10 seconds before the Session timeout. It also presents the user with a button "Refresh Session". When the "Refresh Session" button is clicked an Ajax call is made to the "UpdateSession" method. There is NO code inside the UpdateSession method. The call/request renews the session for another 1 minute.
This technique helps us to notify the users that their session is about to expire so if they like to continue they must refresh/renew their session. ";
In the above code I am setting the Session.Timeout to "1" minute. After that I put "Tomas andro" in the Session object. This means that if you don't make request to the server and sit idle then after 1 minute you won't be able to access the "Name" key in the Session bag. A good scenario will be a user filling out a long form and you store the values of the form in a Session object. The user fills only couple of fields and then sit idle for 25 minutes keeping in mind that the Session timeout is 20 minutes. Now, if the user tries to retrieve something from the Session object after 25 minutes it will throw the ArgumentNullException as the Session object has been removed.
The question is how do we renew the Session and how do we notify the user that his session is about to expire. The idea is to use JavaScript and notify the user 10 seconds before the Session is about to expire.
First, here is the code from the TestPage.aspx.cs:
Code:
public partial class TestPage : BasePage { protected void Page_Load(object sender, EventArgs e) {
protected void Button2_Click(object sender, EventArgs e) { if (Session["Name"] == null) throw new ArgumentNullException("Session[Name] is null"); } }
With the click of a button I insert my name "Tomas andro" into the Session object. The Session.Timeout is 1 minute and is configured in the web.config file.
Here is the code for the BasePage.cs:
Code:
public class BasePage : System.Web.UI.Page {
public BasePage() { this.Load += new EventHandler(BasePage_Load); }
When the page loads I register the page to use AJAXPRO.NET library and inject the timer script. The InjectTimerScript method injects the setInterval method which fires 10 seconds before the Session timeout. It also presents the user with a button "Refresh Session". When the "Refresh Session" button is clicked an Ajax call is made to the "UpdateSession" method. There is NO code inside the UpdateSession method. The call/request renews the session for another 1 minute.
This technique helps us to notify the users that their session is about to expire so if they like to continue they must refresh/renew their session.
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )
Gordanak
Question subject: Re: how to write code message to user before session will expire
Posted: Tue Jan 13, 2009 5:12 am
Joined: Tue Dec 30, 2008 12:55 am Posts: 3 Has thanked: 0 time Have thanks: 0 time
Thank you for answer. I am not using AJAC, so I find next solution. 1. In the Page_Load function (can be Master page too) I wrote next code : Response.AppendHeader("Refresh", Convert.ToString(Session.Timeout * 1) & "; URL=Timeout.aspx") 2. In the Timeout.aspx after before </head > I wrote next code: <script type="text/javascript" language="javascript"> function endSession() { alert("Your session has expired. You will be redirected to the login page."); url = '<%=mstrLoginURL %>'; document.location=url; } registerLoadTask('endSession()') </script>
3) In the Timeout.aspx.vb in the Page_Load function I wrote next script: If Not IsPostBack Then
mstrLoginURL = ResolveUrl("LogIn.aspx") End If So it is working. Thank you for your suggestion. I will try your suggestion when I will start to use VB.Net 2008.
jingle33
Question subject: Re: how to write code message to user before session will expire
Posted: Mon Aug 17, 2009 2:21 pm
Joined: Mon Aug 17, 2009 2:08 pm Posts: 2 Has thanked: 0 time Have thanks: 0 time
Thanks! was looking for same problem.
doyoubuzz
Question subject: Re: how to write code message to user before session will expire
Posted: Wed Sep 22, 2010 10:37 am
Joined: Tue Sep 07, 2010 9:18 am Posts: 30 Has thanked: 0 time Have thanks: 1 time
yaa...really thanks.... from so many days i am also looking for dis solution...