Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to set session timeout in asp.net and if the session timeout the page has to redirect to login.aspx
Posted

Please refer following url,

After successful login add following code to add user data in session.
C#
HttpContext.Current.Session["UserID"] = data.UserId;
HttpContext.Current.Session["Email"] = data.Email;


Add following code in master page.
C#
public bool checkAuthentication
    {
        get;
        set;
    } 

protected void Page_Load(object sender, EventArgs e)
    {        
        if (checkAuthentication)
        {
            if ((Session["UserID"] + "") == "")
            {
                Response.Write("<script type=\"text/javascript\">" +
                        "window.parent.location = '" + ConfigurationManager.AppSettings["SiteUrl"] + "login?loginUrl=' + window.parent.location + '&mode=session';" +
                                "</script>");
                Response.End();
            }
        }
}


Add following code in each child page where you want to authenticate user session.

C#
protected void Page_PreInit(object sender, EventArgs e)
    {
        this.Master.checkAuthentication = true;
    }


Hope this may help you.
 
Share this answer
 
By default, Session timeouts are set to expire in ASP.NET in 20 minutes. To increase the timeout or expiry you should change the timeout attribute for SessionState in the web.config file

C#
<sessionState  timeout="40" />


Note that if you are using Forms authentication, the Forms timeout setting will log the user out after the set timeout period so you will also have to adjust this attribute:

HTML
<authentication mode="Forms">
          <forms timeout="40"/>
    </authentication>



Redirecting user to login page after session timeout is similar to refreshing the page after certain intervals method. Only thing which will differ is that calculating time after which the page has to be redirected. Hence time can be calculated using Session.timeout property which will give us session timeout value for that session. Add some grace timings to that value and redirect the user to the login page automatically.

Using Window.setTimeout method
In the page Load event:
C#
body.Attributes.Add("onLoad", "window.setTimeout(""window.location.href='login.aspx'""," & (Session.Timeout * 60 * 1000) + 10000 & ");")


Using Meta Tag - Refresh

C#
Response.AppendHeader("Refresh", Convert.ToString((Session.Timeout * 60) + 10) & "; URL=Login.aspx")

Both these methods will redirect the user to login page after session timeout + 10 seconds. This is how you can redirect the user to login page after session timeout without user interaction.
 
Share this answer
 
v2
In your web.config write this:

XML
<configuration>
  <system.web>
     <sessionState timeout="20"></sessionState>
  </system.web>
</configuration>



hope it helps :)
 
Share this answer
 
Comments
Ramu15 22-Mar-12 1:23am    
I have to redirect the page to login.aspx i already made the changes in web.config but no use
 
Share this answer
 
Hi ,

Try Using This In Web.Config To Increase Your Request Length:

<system.web>
    <httpruntime maxrequestlength="2097151">
     enable = "True" executionTimeout="45" />
  </httpruntime></system.web>


If You Want To Redirect To Login Page On Session TimeOut Then:

Create a Session Object Like This :
HttpContext.Current.Session["IsTimeOut"] = true


Check this Session Object and Redirect to any page as your required..

Hope This May Help You..
 
Share this answer
 
v2
Hi,

You can increase session time out in IIS

Application pool=>right click site=>Advance setting=>process model=>Ideal time out(minutes)


by default there is 20 minute, you increase or decrease session time here.
 
Share this answer
 
Go to global.asax file and write the code below:

void Session_Start(object sender, EventArgs e)
{
Session.Timeout = 60;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900