Well, I figured it out myself.
So basically, this was not the βredirectβ I needed. I was looking for the wrong place to solve my problem. I knew that redirection means that I would have to make some client / server trips just to return a json result, and that didnβt like it.
It took me a while to realize that I can pass any type of result to filterContext.Result.
It's my fault. I did not ask the right question because I did not quite understand the problem I was facing. After a lot of research, it comes down to being really stupid.
Final decision:
public class CheckLoginAttribute : AuthorizeAttribute, IAuthorizationFilter { private RolesEnum expectedRole; public CheckLoginAttribute(RolesEnum role) { expectedRole = role; } public override void OnAuthorization(AuthorizationContext filterContext) { User user = filterContext.HttpContext.Session["user"] as User; if (user == null || user.Role != expectedRole) { filterContext.Result = new JsonResult() { JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data = new LoginMessage() { IsValidLogin = false } }; } } }
And now I can decorate my actions with this:
[CheckLogin(RolesEnum.Admin)]
A bit more code to avoid stealing an ASP.NET session, and I ended up.
Hope this helps someone out there. Thanks.
Tchiyuan
source share