views:

27

answers:

1

I wrote an ASP.NET page that requires HTTP Basic authorization, which I put in the Page_Load function:

void Page_Load(object sender, EventArgs e)
{
   string auth = Request.Headers["Authorization"];     

   if (string.IsNullOrEmpty(auth))
   {
      Response.StatusCode = 401;
   }
   else
   {  
      string[] usernameAndPassword = Encoding.UTF8.GetString(Convert.FromBase64String(auth)).Split(':');
      string username = usernameAndPassword[0];
      string password = usernameAndPassword[1];         
      Login(username, password);
   }
}

When I try to view the page in a browser (either Firefox or IE), it asks me for the username and password, and then...asks me for the username and password again.

Why does this happen, and how can I fix it?

+1  A: 

This should be handled though a httpmodule. Please consider following article. http://www.codeproject.com/KB/web-security/AspNetCustomAuth.aspx

Fahad