具有安全修整功能的应用程序菜单 [英] Application Menu with Security Trimming

查看:61
本文介绍了具有安全修整功能的应用程序菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自asp.net 2.0网络表单;我只在Web.sitemap中定义菜单,所有剪裁都已取消.

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

在asp.net-core-mvc中,此看似简单的任务是否具有任何等效功能?

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

推荐答案

您可以为其创建自定义TagHelper,在此标记助手中,您可以检查用户是否处于适当的角色:

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

这篇关于具有安全修整功能的应用程序菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆