1

If I register a global Authorize attribute in FilterConfig.cs so that each action method can only be accessible to authenticated users, and decorate some controllers with [Authorize(Role="Admin")] so that only admin users can access them, does authorization logic run twice on these controllers? What can I do to prevent that?

1

1 Answer 1

1

You can use an ASP.NET MVC "FilterProvider" provider. What this will do is help you to fetch all relevant filters from a specific controller and action.

So you can define your own provider and register that one instead of the default ones. this will give you full control over the asp.net filters and you can remove some filters based on your requirement.

Lets say we have following Controller.

[Authorize]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Whatever()
    {
        return View();
    }

}

I think you are looking a way to do something as follows. concentrate on Index Action

[Authorize]
public class HomeController : Controller
{
    [ExcludeFilter(typeof(AuthorizeAttribute))] // Excluding Authorize Important !
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Admin() // will follow the declared authorize Attribute
    {
        return View();
    }
}

If thats what you are Looking for then see this Article

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.