Determine if controller action is allowed before calling

The problem that I am trying to solve seems to me very simple: my application has a menu on the site from which the user can navigate through various areas. Menu items are just references to ActionResult methods. I use the custom attribute AuthorizeAttribute for actions related to the execution of user authorization roles. This works great because it prevents the user from completing an action if they do not have the appropriate roles. However, I do not want to show the menu option to go to this action, if he does not have the right to perform it.

So, at the simplest level, I want to be able to do something like this:

var isAuthorized = IsAuthorized("ControllerName", "ActionName", currentUser);

The IsAuthorized method will then look for any AuthorizeAttributes attributes for this action and evaluate whether the user will be able to execute it.

Is there such an approach? Suppose we don’t have a controller instance in the place where we are doing this assessment.

+5
source share
1 answer

The second pass when looking for an approach to this actually led to what I was looking for. I will leave this question here if it leads others to this answer.

Create an Authorized Action Link Extension for ASP.NET MVC 3

The implementation uses roughly the same syntax I was looking for and works in practice.

+5
source

All Articles