Is it possible to use RedirectToAction() inside a custom AuthorizeAttribute class?

Posted by Lance McNearney on Stack Overflow See other posts from Stack Overflow or by Lance McNearney
Published on 2010-03-18T18:58:10Z Indexed on 2010/03/18 20:01 UTC
Read the original article Hit count: 165

Filed under:

Using ASP.Net MVC 2, is there any way to use the RedirectToAction() method of the Controller class inside a class that is based on the AuthorizeAttribute class?

public class CustomAttribute : AuthorizeAttribute {
    protected override bool AuthorizeCore(HttpContextBase context) {
        // Custom authentication goes here
        return false;
    }

    public override void OnAuthorization(AuthorizationContext context) {
        base.OnAuthorization(context);

        // This would be my ideal result
        context.Result = RedirectToAction("Action", "Controller");
    }
}

I'm looking for a way to re-direct the user to a specific controller / action when they fail the authentication instead of returning them to the login page. Is it possible to have the re-direct URL generated for that controller / action and then use RedirectResult()? I'm trying to avoid the temptation to just hard-code the URL.

© Stack Overflow or respective owner

Related posts about asp.net-mvc