473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Set session Timeout and Auto Redirect

Hi,

I am creating web application. In that I want to set session timeout (not idle timeout). If a user logged in that time the session time will start and it automatically should detect session timeout the page should redirect to another page. How can I acheive this.
Sep 20 '10 #1
1 5141
Frinavale
9,735 Expert Mod 8TB
Since this code is going to be on every page in your website I recommend that you create a "base page class" that all of your pages can inherit from. This way you can implement the code that checks Session once instead of having to copy/paste the code into every page.

That being said, I recommend that you put something into Session when the user logs in. That way you can retrieve it from Session and check if it is Null/Nothing to determine whether or not Session has ended.

So, when the user logs in set something in Session like:
(VB.NET)
Expand|Select|Wrap|Line Numbers
  1. Dim userName As String = "name"
  2. Session("UserName") = userName
(C#)
Expand|Select|Wrap|Line Numbers
  1. string  userName = "name";
  2. Session["UserName"] = userName;
Now, in every Page Load event check if anything in Session["UserName"] exists...if it doesn't then you know their Session has expired (or otherwise abandoned) and in this case you want to redirect them.
(VB.NET)
Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
  2.   If Session("UserName") Is Nothing Then
  3.     Response.Redirect("otherPage.aspx",true)
  4.   End If
  5. End Sub
  6.  
(C#)
Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(Object sender, EventArgs e)
  2. {
  3.   if(Session["UserName"] == null)
  4.   {
  5.     Response.Redirect("otherPage.aspx",true);
  6.   }
  7. }
  8.  
-Frinny
Sep 20 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: HolaGoogle | last post by:
hi all, can you please tell me what i should do to avoid session timeout when displaying my database info in my asp form (DisplayUserDatabase.asp)??? ** actualy it does load and display the...
7
by: Jack | last post by:
Hi, In my application, the main page is accessed via a login screen. A particular record is accessed based on the value of login page. Everything works fine. However, if this screen is kept idle...
12
by: Geigho | last post by:
Setting session timeout in web.config file does not seem to have any effect. Any explanation or suggestion will be appreciated.
6
by: Weave | last post by:
I would like to redirect to a logout page after a session has timed out. I have placed a response.redirect "loggedoff.asp" in the Session_OnEnd subroutine in the global.asa, but it does not move...
8
by: Joe Abou Jaoude | last post by:
hi, I have a web app with forms authentication and a timeout session of 20 mins for security reasons. I recently added a feature that allows users (if they want to) to automatically log in...
3
by: hn | last post by:
I tried to set session timeout =1 minute as seen below in my Web.config but it didn't work. I also tried to set the timeout in IIS as well and still the application doesn't time out after 1 minute....
20
by: Simon Says | last post by:
Hi, I've a login page in which after authenticating it via the Oracle DB, I will stored the user information into the Session. However, when the Session timeout occurs, all of the user...
3
by: Mufasa | last post by:
Folks, I'm having problems with my session timeout. People using my website leave it just sitting there while they do other things. They have logged in ( using Forms Authentication ) and will be...
7
by: Microsoft Newsserver | last post by:
Hi Folks. I have an issue I need some help with if thats OK. I am running Framework 2.0 using Windows Integrated Security. For most of the application we manage session timeouts without the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.