Best way to abort/cancel action from ActionFilter
I\'ve got this ActionFilter
, and it\'s suppose to end the connection immediately and return a 401 Unau
The answer that @OdeyinkaOlubunmi is correct for Web API or specifically System.Web.Http.Filters.ActionFilterAttribute
but it can't be used for System.Web.Mvc.ActionFilterAttribute
. AuthorizeAttribute
and overriding AuthorizeCore
is a good way to go but if you use @Vadim's example for a GlobalFilter you will end up with the following error in a standard configuration:
HTTP Error 404.15 - Not Found The request filtering module is configured to deny a request where the query string is too long.
This is because the default /Login?ReturnUrl=
will keep appending new values until the query string causes an exception.
The way I have solved it for MVC is like this:
public class DebugActionFilter : System.Web.Mvc.ActionFilterAttribute
{
public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext actionContext)
{
actionContext.Result = new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
return;
}
}