title: Global.asax Session_Start() endless loop
body: <p>In my Global.asax file, I have the following code that deals with with unhandled exceptions:</p>
  
  <pre><code>void Application_Error(object sender, EventArgs e)
  {
      Application["Exception"] = Server.GetLastError();
      Response.Redirect("ErrorPage.aspx");
  }
  </code></pre>
  
  <p>(ErrorPage.aspx retrieves the exception details from Application, and deals with it.)</p>
  
  <p>Still in Global.asax, I run the following code to check all sorts of user-related stuff:</p>
  
  <pre><code>void Session_Start(object sender, EventArgs e)
  {
      ... // All sorts of user-related stuff
  }
  </code></pre>
  
  <p>Normally, both the error redirecting and the user-related-stuff work perfectly fine.</p>
  
  <p>However, if ever an error is thrown during <code>Session_Start</code>, the page is redirected, and <strong>Session_Start is called all over again</strong>!</p>
  
  <p>This results in an endless loop.</p>
  
  <p>From what I have found <a href="http://www.velocityreviews.com/forums/t300297-global-asax-redirect-issue.html" rel="nofollow">here</a> and <a href="http://www.velocityreviews.com/forums/t96679-response-redirect-in-application_start-and-session_start.html" rel="nofollow">here</a>, this happens because of some sort of <a href="/questions/tagged/session-cookies" class="post-tag" title="show questions tagged 'session-cookies'" rel="tag">session-cookies</a> issue, which makes the browser and/or server think that the session is restarted at every redirect.</p>
  
  <p>Does anyone know how to resolve the cookie issue?</p>