I have read many forum posts regarding how to redirect a page to a Login Page after the session has expired. One post provided the proper solutions so I decided to write an article on this topic.
There are many responses for my previous article Login Page in ASP.Net C# Using Stored Procedure having 176 K + views, so if you want the details of how to create a Login form in-depth then please refer to that article because in this article I am not going to discus it in detail.
So let us start creating web application as:
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New Project" - "C#" - "Empty Project" (to avoid adding a master page).
- Provide the web site a name such as "SessionTimeOut"or another as you wish and specify the location.
- Then right-click on Solution Explorer - "Add New Item" -Add three Web Forms
- Add Global.asax file if not added
- Drag and drop one button and two textBoxes on the <form> section of the Login.aspx page.
Now open the Web.config file and set the Session Timeout to 1 minute as in:
Now open the Global.asax file and write the following code for the Session_Start Event:
In the code above of the Global.asax file if the session is null then the page is redirected to Login.aspx. If not then it redirects to Welcome.aspx. I did this logic in the Global.asax file because the Global.asax file events are fired globally.
Now the entire Global.asax file will be as follows:
Now open the Login.aspx.cs page and write the following code in the Login Button click:
In the code above, first we are storing Login User Name in the Session so we can get Login User Name at the Next Page and then we redirect the page to the Welcome.aspx Page.
Now write the following code in the Welcome.aspx.cs page as:
In the code above, after the user logs into the application it is redirected to the Welcome.aspx page so we are taking the current user login name on page load from Session and assigned to the label.
I have added another page that is AddUserDetails.aspx and called it from the code above that determines if the session has expired. If it is expired then it will redirect to the AddUserDetails.aspx page otherwise it goes to the Login.aspx page.
Now our application is ready for testing, so let us run the application. The following Login Page will be shown.
Enter credentials and click on the Login Page. It will redirect to the Welcome.aspx as in the following:
Now click on the Goto Add UserPage button. It will redirect to the AddUser.aspx page as in the following:
Now keep ideal for 1 minute without clicking anywhere and after 1 minute click on the Back To Welcome Page button, it will be redirect you to the login page as in the following:
Now from all the above examples, we have learned how to redirect the user to a login page after the session has expired.
- This is not a complete login page Implementation technique, this is only to provide an idea of how to redirect a page to a Login page after the session expires.
From all the examples above, we have learned how to redirect the user to a Login page after the session has expired. I hope this article is useful for all readers, if you have a suggestion then please contact me.