首页 > 解决方案 > 检查模型中的用户角色以与“if”语句一起使用

问题描述

寻求一点帮助。我有一个仪表板,它根据角色在侧面填充一个菜单。它使用 RoleId 来填充。这是在一个类中完成的。我发现如果某些角色已登录,我需要在仪表板链接中添加一个 userId。所以我希望使用一个if语句,但是即使我有using Microsoft.AspNet.Identity;

if (User.IsInRole("Admin")){ } 

不管用。它说这User是未定义的。我已经http.Context在课堂上使用过,但我不确定如何使用我在if声明中的内容。目前我正在返回当前用户的角色列表。不知道如何将其纳入if声明。

下面是我的代码:

public static MvcHtmlString GetMenuBarPage(Guid? ParentId)
    {
        StringBuilder sb = new StringBuilder();
        GeneralEntities db = new GeneralEntities();
        var userId = HttpContext.Current.User.Identity.GetUserId();
        var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
        List<string> roles = userManager.GetRoles(userId).ToList();
        IEnumerable<string> currentUserRoles = userManager.FindById(HttpContext.Current.User.Identity.GetUserId()).Roles.Select(r => r.RoleId);
        var cacheItemKey = "tylrwb" + userId + "Us" + currentUserRoles;

        var global = HttpRuntime.Cache.Get(cacheItemKey);
        if (global == null)
        {
            // Added IsForNavbar Pre-dates 02/09/2020
            global = db.MenuPermissions.Where(i => currentUserRoles.Contains(i.RoleId)
             && (i.UserId == userId || i.UserId == null) && i.IsForNavBar != false).ToArray();
            HttpRuntime.Cache.Insert(cacheItemKey, global, null, DateTime.Now.AddMinutes(2), System.Web.Caching.Cache.NoSlidingExpiration);
        }

       

        sb.Append("<ul class=\"sidebar-menu \"data-widget=\"tree\">");
        sb.Append("<li class=\"header\">MAIN NAVIGATION</li>");
        if (roles == "Admin")
        {
            sb.Append("<li> <a href=\"" + MicrosoftHelper.MSHelper.GetSiteRoot() + "/Dashboard\"> <i class=\"fa fa-line-chart\"></i> <span>Dashboard</span> </a> </li>");
            sb.Append("<li> <a href=\"" + MicrosoftHelper.MSHelper.GetSiteRoot() + "/Analytics\"> <i class=\"fa fa-line-chart\"></i> <span>Analytics</span> </a> </li>");
        }
        else
        {
            sb.Append("<li> <a href=\"" + MicrosoftHelper.MSHelper.GetSiteRoot() + "/Dashboard/" + HttpContext.Current.User.Identity.GetUserId() + "\"> <i class=\"fa fa-dashboard\"></i> <span>Dashboard</span> </a> </li>");
        }
            
       
        sb.Append(GetMenuBar(ParentId, (MenuPermission[])global));
        sb.Append("</ul>");
        return MvcHtmlString.Create(sb.ToString());
    }

标签: c#asp.netmodel-view-controller

解决方案


我抓住了一个机会,将代码放在 aforeach中以在列表中运行,它可以工作。有时我的大脑会一片空白。希望这对其他人有帮助!

 public static MvcHtmlString GetMenuBarPage(Guid? ParentId)
    {
        StringBuilder sb = new StringBuilder();
        GeneralEntities db = new GeneralEntities();
        var userId = HttpContext.Current.User.Identity.GetUserId();
        var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
        List<string> roles = userManager.GetRoles(userId).ToList();
        IEnumerable<string> currentUserRoles = userManager.FindById(HttpContext.Current.User.Identity.GetUserId()).Roles.Select(r => r.RoleId);
        var cacheItemKey = "tylrwb" + userId + "Us" + currentUserRoles;

        var global = HttpRuntime.Cache.Get(cacheItemKey);
        if (global == null)
        {
            // Added IsForNavbar Pre-dates 02/09/2020
            global = db.MenuPermissions.Where(i => currentUserRoles.Contains(i.RoleId)
             && (i.UserId == userId || i.UserId == null) && i.IsForNavBar != false).ToArray();
            HttpRuntime.Cache.Insert(cacheItemKey, global, null, DateTime.Now.AddMinutes(2), System.Web.Caching.Cache.NoSlidingExpiration);
        }

     

        sb.Append("<ul class=\"sidebar-menu \"data-widget=\"tree\">");
        sb.Append("<li class=\"header\">MAIN NAVIGATION</li>");
        foreach (var role in roles)
        {
            if (role == "Administrator")
            {
                sb.Append("<li> <a href=\"" + MicrosoftHelper.MSHelper.GetSiteRoot() + "/Dashboard\"> <i class=\"fa fa-line-chart\"></i> <span>Dashboard</span> </a> </li>");
                sb.Append("<li> <a href=\"" + MicrosoftHelper.MSHelper.GetSiteRoot() + "/Analytics\"> <i class=\"fa fa-line-chart\"></i> <span>Analytics</span> </a> </li>");
            }
            else
            {
                sb.Append("<li> <a href=\"" + MicrosoftHelper.MSHelper.GetSiteRoot() + "/Dashboard/" + HttpContext.Current.User.Identity.GetUserId() + "\"> <i class=\"fa fa-dashboard\"></i> <span>Dashboard</span> </a> </li>");
            }
        }


        sb.Append(GetMenuBar(ParentId, (MenuPermission[])global));
        sb.Append("</ul>");
        return MvcHtmlString.Create(sb.ToString());
    }

推荐阅读