Asp.Net Core MVC在View中,根据用户权限动态生成菜单

 

1.   用户登录时,将用户的权限写入Cookie;

1 //将需要的信息写入claims后
2 var identity = new ClaimsIdentity(claims, IdentityConstants.ApplicationScheme);
3 var userPrincipal = new ClaimsPrincipal(identity);
4 await HttpContext.SignInAsync(IdentityConstants.ApplicationScheme, userPrincipal);

2.  View中,利用bootstraptreeview显示菜单,使用User.IsInRole等判断是否有相关权限,进而确定是否需要显示该部分的菜单

//部分代码
<div id="leftmenu"></div>
<script>
    var tree = new Array();
    @if (User.IsInRole("**")||(User.HasClaim("单位","***")&&User.IsInRole("部门管理员")))
    {
        <text>
            tree.push({
                text: "***",
                selectable: false,
                nodes:
                    [
                        {
                            text: "***",
                            selectable: false,
                            nodes:
                                [
                                    {
                                        text: "***",
                                        href: "***",
                                    },
                                    。。。
                                ],
                        },
                        {
                            text: "***",
                            selectable: false,
                            nodes:
                                [
                                    {
                                        text: "当班清单",
                                        href: "/Spectroes/Index",
                                    },
                                    。。。
                                ],
                        },
                        @if (User.IsInRole("部门管理员"))
                        {
                            <text>
                                {
                                    text: "审核上传",
                                    href: "Audities",
                                    selectable: true,
                                },
                                。。。。
                            </text>
                        }
                    ]
            });
        </text>
    }

    @if (User.IsInRole("超级管理员"))
    {
        <text>
            tree.push(
                    {
                        text: "***",
                        selectable: false,
                        nodes:
                            [
                                {
                                    text: "***",
                                    selectable: false,
                                    href: "***",
                                },
                            ]
                    },
        </text>
    }
    $('#leftmenu').treeview({
        data: tree,
        icon: "glyphicon glyphicon-stop",
        color: "#000000",
        backColor: "#FFFFFF",
        collapseIcon: "glyphicon glyphicon-minus",
        checkedIcon: "glyphicon glyphicon-check",
        levels: 1,
        enableLinks: true,
        showBorder: false,
    });
</script>            

 

posted @ 2019-10-11 18:15  jqdy  阅读(891)  评论(0编辑  收藏  举报