I am looking to reuse my currently defined Controller / Action Authorize attributes, which define user access roles for displaying menu items (so that the user only sees the menu items that they have access to).
Currently, the display of menu items and the role of the authorization / action attribute is authorized, so any changes will require updating in two places, which may be subject to errors in the future.
I have been studying user authorization attributes, and this is what I still have:
public class MyAuthorizeAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { var isAuthorized = base.AuthorizeCore(httpContext); if (!isAuthorized) { return false; } var routeData = httpContext.Request.RequestContext.RouteData; string currentAction = routeData.GetRequiredString("action"); string currentController = routeData.GetRequiredString("controller"); var currentUserRoles = GetCurrentUserRoles();
MenuItems essentially consist of user roles available for a menu item, a text label for the external interface, and the controller URL and action. MenuItems are displayed in a partial view, depending on whether the current user is in the required role to display the MenuItem.
From what I see, I may need an exhaustive list of all controller actions and related user roles that will be reused in both areas, is there a more elegant way to achieve this?
source share