I have an secure application with an Authorize
attribute on each action.
[Authorize(Roles = "Role1,Role2")]
public ActionResult MyAction(int id)
{
return View();
}
In my UI, I have links to these controller/actions. I would like to create a custom HtmlHelper for the links that accepts controller and action names:
@Html.SecuredLink("Click Me", "MyAction", "MyController");
And this would determine weather to render itself or not based on if the user has permission to the given action:
public static MvcHtmlString SecuredLink(this HtmlHelper helper, string text, string action, string controller)
{
var userId = Membership.GetUserId();
var userHasRightsToThisAction = IsActionAccessibleToUser(helper.ViewContext.RequestContext.HttpContext, controller, action); // <- How would this work?
if (userHasRightsToThisAction )
{
// Render Link
// ...
}
}
I have been unable to find a way to easily test the action from code for authorization status.