Application Menu with Security Trimming

夙愿已清 提交于 2019-12-11 09:19:59

问题


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 ?


回答1:


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();
    }
}


来源:https://stackoverflow.com/questions/42240979/application-menu-with-security-trimming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!