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:
<system.web> <sessionState mode="InProc" timeout="1"/> </system.web>
Now open the Global.asax file and write the following code for the Session_Start Event:
void Session_Start(object sender, EventArgs e) { // Code that runs when a new session is started if (Session["LoginUserName"] != null) { //Redirect to Welcome Page if Session is not null Response.Redirect("Welcome.aspx"); } else { //Redirect to Login Page if Session is null & Expires Response.Redirect("Login.aspx"); }
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:
<%@ Application Language="C#" %> <script RunAt="server"> void Application_Start(object sender, EventArgs e) { // Code that runs on application startup } void Application_End(object sender, EventArgs e) { // Code that runs on application shutdown } void Application_Error(object sender, EventArgs e) { // Code that runs when an unhandled error occurs } void Session_Start(object sender, EventArgs e) { // Code that runs when a new session is started if (Session["LoginUserName"] != null) { //Redirect to Welcome Page if Session is not null Response.Redirect("Welcome.aspx"); } else { //Redirect to Login Page if Session is null & Expires Response.Redirect("Login.aspx"); } } void Session_End(object sender, EventArgs e) { // Code that runs when a session ends. // Note: The Session_End event is raised only when the sessionstate mode // is set to InProc in the Web.config file. If session mode is set to StateServer // or SQLServer, the event is not raised. } </script>
Now open the Login.aspx.cs page and write the following code in the Login Button click:
protected void Button1_Click(object sender, EventArgs e) { Session["LoginUserName"] = Convert.ToString(TextBox1.Text); Response.Redirect("Welcome.aspx"); }
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:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Label1.Text="WelCome " +Convert.ToString(Session["LoginUserName"]); } protected void Button1_Click(object sender, EventArgs e) { Response.Redirect("AddUserDetails.aspx"); } }
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.
Note:
- 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.
Summary
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.
Post a Comment