Remove HTTP (unauthorized) exception from Application_Error

I am trying to throw this line from Global asax Application_Error

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

But for some reason I get 200, 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");
}
+5
source share
1 answer

This code can help you.

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

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

+1
source

All Articles