title: Http module Authentication
body: <p>I'm using the code below from <a href="https://stackoverflow.com/questions/8809717/how-to-implement-basic-http-authentication-with-the-vs-debug-server/8811020#comment42412853_8811020">this post</a> to try and create a custom http module :</p>
  
  <pre><code>public class BasicAuthenticationModule: IHttpModule
  {
    public void Init(HttpApplication application)
    {
      application.AuthenticateRequest += new EventHandler(Do_Authentication);
    }
  
    private void Do_Authentication(object sender, EventArgs e)
    {
      var request = HttpContext.Current.Request;
      string header = request.Headers["HTTP_AUTHORIZATION"];
      if(header != null &amp;&amp; header.StartsWith("Basic "))
      {
        // Header is good, let's check username and password
        string username = DecodeFromHeader(header, "username");
        string password = DecodeFromHeader(header, password);
  
        if(Validate(username, password) 
        {
          // Create a custom IPrincipal object to carry the user's identity
          HttpContext.Current.User = new BasicPrincipal(username);
        }
        else
        {
          Protect();
        }
      }
      else
      {
        Protect();
      }
    }
  
    private void Protect()
    {
      response.StatusCode = 401;
      response.Headers.Add("WWW-Authenticate", "Basic realm=\"Test\"");
      response.Write("You must authenticate");
      response.End();
    }
  
    private void DecodeFromHeader()
    {
      // Figure this out based on spec
      // It's basically base 64 decode and split on the :
      throw new NotImplementedException();
    }
  
    private bool Validate(string username, string password)
    {
      return (username == "foo" &amp;&amp; pasword == "bar");
    }
  
    public void Dispose() {}
  
    public class BasicPrincipal : IPrincipal
    {
      // Implement simple class to hold the user's identity
    }
  }
  </code></pre>
  
  <p>The code works ok at making the 401 error be returned by the server and the login dialog pop up but when the correct login details are entered the login dialog does not go away. </p>
  
  <p>When debugging the code nothing happens when the Ok button on the dialog is clicked, the event isn't triggered and the user details aren't validated, I can't figure out why this isn't working.</p>
  
  <p>Any help or ideas would be great, thanks.</p>