Reputation: 443
I have this code in one of the shard Views:
<li>
<a href="@Url.Action("Index","Campaigns")" class="tables"><span>Campaigns</span></a>
</li>
On the Campaigns Controller I have a custom AuthorizeAttribute
like this:
[AuthorizeRoles(Roles = "admin")]
public class CampaignsController : Controller
{
...
}
The problem is that if the user is not an admin
, the link is still being rendered, even if the controller is not accessible.
I am using a custom implementation and because of this, standard methods of authorization such as .IsInRole
will not work.
Is there a way to apply an attribute to my controller method in such a way that the link is hidden if the user does not have access, without using .IsInRole
?
Upvotes: 0
Views: 856
Reputation: 5318
You can do create your own extension method to extend MvcHtmlString so that Action thing will be rendered based on condition
public static MvcHtmlString IfAllowed(this MvcHtmlString action, bool allowed)
{
return allowed? action:String.Empty;
}
When you use, I dont know how your custom stuff works but pass in the boolean based on your custom role or whatever
<li>
@Html.ActionLink("Index","Champaigns").IfAllowed(true/false)
</li>
Upvotes: 1