Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

asp.net-mvc

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.

like image 959
Lance McNearney Avatar asked Mar 18 '10 18:03

Lance McNearney


People also ask

Can we pass model in RedirectToAction?

My Question - Can I pass student model in RedirectToAction? Since the route dictionary deals with objects, try changing the GetStudent action to accept an object and inside cast it to Student . Another option would be to serialize it using JSON when passing it from FillStudent .

What is difference between RedirectToAction and RedirectToRoute?

RedirectToAction will return a http 302 response to the browser and then browser will make GET request to specified action. Show activity on this post. Ideally I would use RedirectToRoute for Action Links/Images and RedirectToAction in Controller's Action to redirect to another Controller's Action .

What is the usage of a RedirectToAction?

The RedirectToAction() Method This method is used to redirect to specified action instead of rendering the HTML. In this case, the browser receives the redirect notification and make a new request for the specified action. This acts just like as Response.


2 Answers

You can/should override HandleUnauthorizedRequest instead of OnAuthorization. Here's the default implementation:

    protected virtual void HandleUnauthorizedRequest(AuthorizationContext filterContext) {         // Returns HTTP 401 - see comment in HttpUnauthorizedResult.cs.         filterContext.Result = new HttpUnauthorizedResult();     } 

You can't use Controller.RedirectToAction, but you can return a new RedirectToRouteResult.

So you can do:

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) {         // Returns HTTP 401 - see comment in HttpUnauthorizedResult.cs.         filterContext.Result = new RedirectToRouteResult(                                    new RouteValueDictionary                                     {                                        { "action", "ActionName" },                                        { "controller", "ControllerName" }                                    });     } 
like image 153
Craig Stuntz Avatar answered Sep 20 '22 15:09

Craig Stuntz


You can do something like this:

var routeValues = new RouteValueDictionary(); routeValues["controller"] = "ControllerName"; routeValues["action"] = "ActionName"; //Other route values if needed. context.Result = new RedirectToRouteResult(routeValues); 

This is the way the framework does it when you call "RedirectToAction()" in your controller.

like image 44
Mattias Jakobsson Avatar answered Sep 17 '22 15:09

Mattias Jakobsson