What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 16317 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Redirection to Login page after session time out

Redirection to Login page after session time out

2 vote(s)
Rating: 4.5 out of 5
Article posted by Amit.jain on 1/5/2009 | Views: 27076 | Category: ASP.NET | Level: Intermediate red flag


In this example i'll show how to detect the session timeout which occurs when user is idle for the time specified as Session.Timeout,using C# asp.NET and if it is than redirect the user to login page to login again, for this i've set time out value in web.config file to 1 minute

Download


 Download source code for Redirection to Login page after session time out


Setting up web.config for forms authentication
In web.config file, set the sessionstate mode to inproc and authentication mode to Forms
<system.web>
<compilation debug="true"/>
<authentication mode="Forms"/>
<sessionState mode="InProc" cookieless="false" timeout="1">
</sessionState>
</system.web>
I've created three pages in this example , one is login page , when session expires , i redirect to this page , one is navigation page where i'll check if session is valid or not , if it is valid than only user will see this page other wise he gets redirected to login page
Code for Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>


<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSessionStart"
runat="server"
OnClick="btnSessionStart_Click"
Text="Start Session" /><br />
<br />
<br />
<asp:Button ID="btnCheck"
runat="server"
OnClick="btnCheck_Click"
Text="Check Session ID" />
<br />
<asp:TextBox ID="txtSession"
runat="server"
Width="266px">
</asp:TextBox><br />
<br />
<asp:Button ID="btnGO"
runat="server"
OnClick="btnGO_Click"
Text="Go to Other Page" />
<br />
<br />
</div>
</form>
</body>
</html>
And the code behind for this page is like
protected void btnSessionStart_Click(object sender, EventArgs e)
{
Guid Session_id = Guid.NewGuid();
Session["SessionID"]= Session_id.ToString();

}

protected void btnCheck_Click(object sender, EventArgs e)
{
if (Session["SessionID"] != null)
txtSession.Text = Session["SessionID"].ToString();
else
txtSession.Text = "Session has expired";
}

protected void btnGO_Click(object sender, EventArgs e)
{
Response.Redirect("Default2.aspx");
}
Now the page where we want to check the session has timed out or not, we need to check it in the Page_Init event of the page , is session is not null than only user will be able to go to the page other wise he will get redirected to login page
In this page I've just put a button to go to home page

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnHome"
runat="server" OnClick="btnHome_Click"
Text="Home" /></div>
</form>
</body>
</html>

And the Code behind for this page is

protected void Page_Init(object sender, EventArgs e)
{
CheckSession();
}
protected void btnHome_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}

private void CheckSession()
{
if (Session["SessionID"] == null)
{
Response.Redirect("Login.aspx");
}

}
Conclusion
If we need to check this in all the pages of application than we can create a BaseClass and write the above mentioned code of CheckSession and Page_Init part and drive all ur pages from this class by typing BaseClassName in place of System.Web.UI.Page and it will check all pages for session timeout every time page is loaded

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About amiT jaiN

Experience:4 year(s)
Home page:http://csharpdotnetfreak.blogspot.com/
Member since:Friday, December 26, 2008
Level:Starter
Status: [Member]
Biography:
 Responses
Posted by: Javaexp | Posted on: 20 Dec 2012 11:50:38 AM | Points: 25
>> Write Response - Respond to this post and get points
Related Posts

Its very difficult to remember to all the country name. This article will collect all the country name and bind to a DropDownList. Its very simple class just import this Country.cs in your application and get all the country name.

In this article we shall learn How to create a slide show images from the database in ASP.NET.

This one is really very useful whenever we have a need to calculate running totals in the grid either row wise or in column wise.

In earlier articles, we learnt how to create, read, expire cookies. In this article we are going to learn how to limit the scope of the cookies to a particular folder of the website or particular sub-domain of the website.

This article shows how to get Selected record key value from GridView, DataList, FormView, DetailsView, ListView, and Repeater controls.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 4:50:27 PM