Sunday, December 29, 2013

Detecting Session Timeout And Redirect To Login Page In ASP.NET

1)put this code inside master page page_load event.
 protected void Page_Load(object sender, EventArgs e)
    {
        
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.Cache.SetNoServerCaching();
        HttpContext.Current.Response.Cache.SetNoStore();
        HtmlMeta meta = new HtmlMeta();
        meta.HttpEquiv = "Refresh";
        meta.Content = Convert.ToString(Session.Timeout * 60) + ";url=" + ResolveUrl("~/login.aspx");
        this.Page.Header.Controls.Add(meta);
			
    }
Web.Config
-----------------
 <system.web>
<compilation debug="true"/>
<authentication mode="Forms">
<forms name="TestAppAuth" loginUrl="Login.aspx" timeout="2880"/>
</authentication>
 <sessionState mode="InProc" cookieless="false" timeout="1" />
</system.web>
(timeout has been set to 1 min for testing purpose,change it according to your need)

0 comments:

Post a Comment