Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make AuthorizeAttribute respond with status code 403 Forbidden rather than a redirect?

If the user is not logged in and they request an action marked [Authorize], then the response is a redirect to the Account/LogOn action (status code 302 Found).

Is there a way to make the response be status code 403 Forbidden instead?

like image 752
Daniel Trebbien Avatar asked Jan 19 '11 20:01

Daniel Trebbien


1 Answers

Create an action filter that inherits from AuthorizeAttribute. Then override this method:

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{ 
   Response.StatusCode = 403;
   Response.Status = "Forbidden";
   Response.StatusDescription = "Forbidden";
   Response.End();
   Response.Close();

}
like image 157
Chris Kooken Avatar answered Sep 21 '22 00:09

Chris Kooken