2

I came from asp.net 2.0 webforms; where i just define my menu in Web.sitemap with all the trimming taken care off.

Is there any equivalent feature in asp.net-core-mvc for this seemingly easy task ?

KM Fong
  • 21
  • 2

1 Answers1

0

You can create a custom TagHelper for it, inside this tag helper you can check whether user is in apporperiate role or not:

public class SecurityTrimmingTagHelper : TagHelper
{
    [ViewContext]
    public ViewContext Context { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        output.TagName = null;

        if (!Context.HttpContext.User.Identity.IsAuthenticated)
        {
            output.SuppressOutput();
        }

        if (Context.HttpContext.User.IsInRole("Admin"))
        {
            return;
        }

        output.SuppressOutput();
    }
}
Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110
  • Hi Sirwan...do I put this in the View that contains the menu ? I am new to mvc... – KM Fong Feb 19 '17 at 04:54
  • @KMFong You should put the markup of the menu inside this custom tag, for example: `This content will be rendered based on the current user permission` – Sirwan Afifi Feb 23 '17 at 05:47