Throw http exception (Unauthorized) from Application_Error
Asked Answered
S

1

6

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");
}
Shoulders answered 18/7, 2012 at 8:51 Comment(3)
Are you have forms auth. enabled in web.config? If you have forms authentication enabled in web.config then you may get this behavior.Mozart
@Mark , Thanks but no I dont.Shoulders
Plz post your Application_ErrorMozart
G
1

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

Geesey answered 25/8, 2017 at 3:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.