title: How to redirect to LogIn page when Session is expired (ASP.NET 3.5 FormsAuthen)
body: <p>I'm using FormsAuthentication and ASP.Net SqlMembership Provider. I would like to provide a functionality to redirect to LogIn page when the underlying Session is expired.</p>
  
  <p>I put the following block of codes in my <strong>BasePage OnInit</strong>. As far as I tested, it always keeps on redirecting to LogIn page even though I supplied correct UserID and Password. By rights, it should take me to default page. </p>
  
  <pre><code>if (Context.Session != null &amp;&amp; Session.IsNewSession &amp;&amp; this.Request.IsAuthenticated)
          {
              string cookieHeader = Request.Headers["Cookie"];
              if (cookieHeader != null &amp;&amp; cookieHeader.IndexOf("ASP.NET_SessionId") &gt;= 0)
              {
  
  
                      HttpContext.Current.Session.Abandon();                       
                      FormsAuthentication.SignOut();                        
                      Response.Redirect(FormsAuthentication.LoginUrl);                                           
  
              }
          }
  </code></pre>