title: Asp.net : HttpApplication context authentication for aspx page gets called infinite times and page never loads
body: <p>I have an asp.net web application that performs some license checks before calling up the login page... if the product is not licensed then it navigates to a abc.aspx page with some error details. This license check is an HttpModule which is configured via web.config.</p>
  
  <p>I have an event handler for context authentication. Whenever the abc.aspx page is called, this event is fired multiple times and the page load never happens.</p>
  
  <p>on Init, i use this code to add the event handler </p>
  
  <pre><code>context.AuthenticateRequest += new EventHandler
  </code></pre>
  
  <p>When i use a html page, this issue does not seem to occur. The issue exists even if i use some other aspx page for example xyz.aspx...</p>
  
  <p>How can stop this authentication to takes place n number of times. I have tried with HttpContext.Current.Response.End(), it stops the infinite calls, but does not load the page, the page appears blank.</p>
  
  <p>Any one has any idea about this issue?</p>
  
  <p>snippet of Global.asax.
  <code></p>
  
  <pre><code>&lt;%@ Application Language="C#" Inherits="Microsoft.Practices.CompositeWeb.WebClientApplication" %&gt;
  
  &lt;script runat="server"&gt;
  
      private static bool _initializedAlready = false;
      private static readonly Object s_lock = new Object();
  
      //fires once on asp.net worker process start
      protected override void Application_Start(object sender, EventArgs e)
      {
  
  
      }
  
      protected void Application_BeginRequest(object sender, EventArgs e)
      {
          if (_initializedAlready)
          {
              return;
          }
  
          lock (s_lock)
          {
  
              if (_initializedAlready)
              {
                  return;
              }
  
              //custom initialization code
              base.Application_Start(sender, e);
  
              _initializedAlready = true;
          }
      }
  
  
  
      public override void Init()
      {
          base.Init();
  
         //initialize the license module here....
  
           licenseModule.Init(this);
  
      }
  
  
  &lt;/script&gt;
  </code></pre>
  
  <p></code></p>
  
  <p>The init() method of license module</p>
  
  <pre><code> public void Init(HttpApplication context)
      {
          context.AuthenticateRequest += new EventHandler(context_AuthenticateRequest);
  
      }
  </code></pre>