Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throw http exception (Unauthorized) from Application_Error

I am trying to throw this row from Global asax Application_Error

throw new HttpException((int)HttpStatusCode.Unauthorized, "Forbidden");

But for some reason I am getting 200 and not 401 in the browser, do you know why?

Update:

protected void Application_Error(object sender, EventArgs e)
{        
    throw new HttpException((int)HttpStatusCode.Unauthorized, "Forbidden");
}
like image 551
SexyMF Avatar asked Jul 18 '12 08:07

SexyMF


1 Answers

This code may help you

 protected void Application_Error(object sender, EventArgs e)
 {
     Response.StatusCode = (int)HttpStatusCode.Unauthorized;
     Server.ClearError();
 }

However, instead of setting status code in Global.asax, you should apply authentication and authorization in web.config

like image 62
Jacob Phan Avatar answered Oct 21 '22 16:10

Jacob Phan