Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override a Global Filter in MVC for One Method

In my filterConfig, I have registered a global attribute filter, which requires authorization on each of my methods.

However, I have one particular method where I want to apply a different authorization filter attribute. How does one accomplish this, if at all possible?

Note: I do not want to use the [AllowAnonymous] attribute (which works seamlessly and completely ignores my filter), since I want the request to be authorized, just through a different set of authorization logic on the method.

like image 520
badazzhindu Avatar asked Mar 27 '13 16:03

badazzhindu


People also ask

Can we override filters in MVC?

ASP.NET MVC 5 has arrived with a very important feature called Filter Overrides. Using the Filter Overrides feature, we can exclude a specific action method or controller from the global filter or controller level filter. ASP.NET MVC 5 has arrived with a very important feature called Filter Overrides.

Can we override the execution order of these filters in MVC?

ASP.NET MVC 5 has a new feature called Filter Overrides, which allows you to clear or replace certain filter types created in higher scopes. For example, if you created a global action filter or controller action filter, you could override those filters on a case-by-case basis at the controller action level.

Can we override action method in MVC?

For MVC action command overrides, extend the BaseMVCActionCommand class, and the only method you'll need to override is doProcessAction , which must return void . It's straightforward to override MVC action commands while keeping your code decoupled from the original action methods.


2 Answers

You can alter your filter to allow multiple by setting AllowMultiple = true in the AttributeUsage attribute on your attribute class, and add a check so that if the filter is present multiple times, the globally-applied one doesn't execute. The ActionExecutingContext that gets passed into OnActionExecuting() lets you get the filters applied via filterContext.ActionDescriptor.GetCustomAttributes(), so you can use that here.

Then, alter the constructor so that you can pass in a parameter (probably an enum) that it can use to decide which authorisation method to use - the normal one, or this other one. Give that parameter a default value that makes it select the normal auth method. Then, on that one method that needs a different auth method, you can apply the filter with the other value of the parameter. So it might look like this:

public class CustomAuthAttribute : AuthorizeAttribute
{
    public CustomAuthAttribute(AuthMethod method = AuthMethod.StandardAuth)
    {
        //stuff
    }
}

[CustomAuth(AuthMethod.WeirdAuth)]
public ActionResult MethodThatNeedsDifferentAuth()
{
    //stuff
}
like image 187
anaximander Avatar answered Oct 13 '22 01:10

anaximander


you can write your own version of the authorize attribute and pass specific parameter to depending on what action would you like your attribute to do for example

public class CustomAuthorizeAttribute : AuthorizeAttribute
   {
        public string currentAction { get; set; }
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
           if (currentAction != "notallowed")
            {
                HandleUnauthorizedRequest(filterContext);
            }
        }
    }

 protected override void HandleUnauthorizedRequest(AuthorizationContext context)
    {
        context.Result = new RedirectResult("/home/login");
    }

and then apply it to your class or action

[CustomAuthorize(currentAction = "notallowed")]
public class HomeController : Controller
like image 40
COLD TOLD Avatar answered Oct 13 '22 00:10

COLD TOLD