title: Forms authentication: disable redirect to the login page
body: <p>I have an application that uses ASP.NET Forms Authentication. For the most part, it's working great, but I'm trying to add support for a simple API via an .ashx file. I want the ashx file to have optional authentication (i.e. if you don't supply an Authentication header, then it just works anonymously). But, depending on what you do, I want to require authentication under certain conditions.</p>
  
  <p>I thought it would be a simple matter of responding with status code 401 if the required authentication was not supplied, but it seems like the Forms Authentcation module is intercepting that and responding with a redirect to the login page instead. What I mean is, if my <code>ProcessRequest</code> method looks like this:</p>
  
  <pre><code>public void ProcessRequest(HttpContext context)
  {
      Response.StatusCode = 401;
      Response.StatusDescription = "Authentication required";
  }
  </code></pre>
  
  <p>Then instead of getting a 401 error code on the client, like I expect, I'm <em>actually</em> getting a 302 redirect to the login page.</p>
  
  <p>For nornal HTTP traffic, I can see how that would be useful, but for my API page, I want the 401 to go through unmodified so that the client-side caller can respond to it programmatically instead.</p>
  
  <p>Is there any way to do that?</p>