I have created custom tag helper to handle this.
[HtmlTargetElement(Attributes = "asp-roles")]
public class SecurityTrimmingTagHelper : TagHelper
{
[ViewContext]
public ViewContext Context { get; set; }
[HtmlAttributeName("asp-roles")]
public string Roles { get; set; }
/// <summary>
/// Hides html element if user is not in provided role.
/// If no role is supplied the html element will be render.
/// </summary>
/// <param name="context"></param>
/// <param name="output"></param>
public override void Process(TagHelperContext context, TagHelperOutput output)
{
if (!Context.HttpContext.User.Identity.IsAuthenticated)
{
output.SuppressOutput();
}
if (!string.IsNullOrEmpty(Roles))
{
var roles = Roles.Split(',');
foreach (var role in roles)
{
if (!Context.HttpContext.User.IsInRole(role))
{
output.SuppressOutput();
return;
}
}
}
}
}
You can apply this to any html element. If you want to apply it to only specific html element ( lets say <li>
) then change the HtmlTargetElement
to
[HtmlTargetElement("li",Attributes = "asp-roles")]
then in view you can do
<li asp-roles="Admin"><a href="/Profile/Admin">Admin</a></li>