【问题标题】:Application Menu with Security Trimming带安全修整的应用程序菜单
【发布时间】:2017-02-16 19:53:57
【问题描述】:

我来自asp.net 2.0 webforms;我只是在 Web.sitemap 中定义了我的菜单,所有的修剪都被取消了。

asp.net-core-mvc 中是否有任何等效功能可以完成这个看似简单的任务?

【问题讨论】:

  • 你可以为它创建一个自定义的TagHelper。
  • Sirwan ...我不明白这个..关于这个的任何站点/示例?

标签: asp.net-core asp.net-core-mvc


【解决方案1】:

你可以为它创建一个自定义的 TagHelper,在这个标签助手中你可以检查用户是否处于适当的角色:

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...我要把它放在包含菜单的视图中吗?我是 mvc 新手...
  • @KMFong 你应该把菜单的标记放在这个自定义标签中,例如:<security-trimming>This content will be rendered based on the current user permission</security-trimming>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多