views:

38

answers:

2

I currently have 2 filters, Auth and Redirect that do the following: Filter Auth, who implements IAuthorizationFilter and ActionFilter, checks for user login and authorization, and if that fails sets the filterContext.Result to be a HttpStatusCodeResult of 403 (forbidden). Filter Redirect, who implements IActionFilter and ActionFilter, checks the Result and if it's a 403, redirects to a login page.

I have them applied to an action as follows:

[Auth(Order=0)]
[Redirect(Order=1)]

However, Auth gets executed but Redirect never gets executed (not one of the 4 overrideable methods it provides). If I remove Auth Redirect gets executed, but if I include Auth as the first filter, Redirect isn't executed. I guess that setting the Result property of the filter context prevents any other filters from getting executed, but I can't realiza why this happens. FYI I'm using ASP.NET MVC 3 beta, but that shouldn't change anything.

Update: Changing the filter type of Auth to IActionFilter instead of IAuthorizationFilter causes the OnResultExecuting and OnResultExecuted in Redirect to fire, but changing the Response there has no effect whatsoever on the final response to the browser.

A: 

When you set HttpStatusCodeResult you are setting a redirect, so the second filter won't execute (because you already have redirected).

eKek0
an HttpStatusCodeResult does not end up being a redirect, it simply tells ASP.NET MVC to serve up a Response with the specified status code.
Diego
A: 

I ended up fixing it by making Redirect a IResultFilter and writing the following in OnResultExecuting:

filterContext.HttpContext.Response.Redirect(url);
Diego