c# - 检查模型中的用户角色以与“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());
}
解决方案
我抓住了一个机会,将代码放在 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());
}
推荐阅读
- html - 关于 robots.txt 禁止参数 URL
- python - xgboost 和 py-xgboost 的区别?
- javascript - Firebase 身份验证。更新所有项目文件
- amazon-ec2 - 使用 IAM 角色的跨账户 SQS 访问
- java - Spark 展平数据集映射的 col
- python - Scrapy - 连续链接上未返回数据
- javascript - 垂直导航 - 最后一个点未激活并为每个点添加标签
- sql - 将双引号添加到带有空格的列的 SQL 输出
- typescript - 打字稿中的逗号是什么意思
- kubernetes - KubeScheduler & Etcd Targets Down - Prometheus Operator