Asp.net mvc 在菜单中应用安全性

Asp.net mvc 在菜单中应用安全性,asp.net-mvc,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 3,下面是我的模型 public class security { public long id { get; set; } public long user_id { get; set; } public long submenu_id { get; set; } public bool flag { get; set; } } 如果标志为true,则用户可以访问子菜单 目前我正在做的是。当用户登录时,此模型中的详细信息将被拉出并存储在单独的会话变量中 eg

下面是我的模型

public class security
{  
    public long id { get; set; }
    public long user_id { get; set; }
    public long submenu_id { get; set; }
    public bool flag { get; set; }

}
如果标志为
true
,则用户可以访问子菜单

目前我正在做的是。当用户登录时,此模型中的详细信息将被拉出并存储在单独的会话变量中

eg: - var c = db.security.where(m=> m.user_id == id).Orderby(id);

if(c.submenu_id == 1 && c.flag == true)
   session["mem_add"] = "true";
else
   session["mem_add"] = "false";
然后在布局视图中检查此会话变量,如果
true
,菜单将显示,但问题是有
16个子菜单
,以后可以增加。因此,我必须为每个子菜单创建单独的会话变量。有没有更好的办法来解决这个问题

已编辑

@if(Session["mem_add"] == "true")
{
          <li class="active"><a href="@Url.Action("Index", "UpdateDetail")">
            <img src="@Url.Content("~/Images/Enrollments.png")" alt=""><span class="title"><b>
    @Resources.Resources.UpdateMyDetail</b> </span> </a></li>
}
@if(会话[“mem_add”]=“true”)
{
  • }
    您的另一个选择是创建对HtmlHleper的扩展,然后您可以使用它将模型传递到菜单,并让它生成一个MvcHtmlString

    public MvcHtmlString CreateMenu(this HtmlHelper helper, MenuSecurityModel)
    {
       //create tags, string, etc
       //create logic add/remove meun items depending on flag property on model
       //this can be accomplished also by using a base class for your Controllers, 
       //set the the onactionexecuting variable to set the menu[ put it in session accodingly, retrieve when appropriate
       //Call using Html.RenderAction , partial, etc
       return yourHtmlString;
    }
    

    您的另一个选项是创建对HtmlHleper的扩展,然后您可以使用它将模型传递到菜单,并让它生成一个MVHtmlString

    public MvcHtmlString CreateMenu(this HtmlHelper helper, MenuSecurityModel)
    {
       //create tags, string, etc
       //create logic add/remove meun items depending on flag property on model
       //this can be accomplished also by using a base class for your Controllers, 
       //set the the onactionexecuting variable to set the menu[ put it in session accodingly, retrieve when appropriate
       //Call using Html.RenderAction , partial, etc
       return yourHtmlString;
    }
    

    您的另一个选项是创建对HtmlHleper的扩展,然后您可以使用它将模型传递到菜单,并让它生成一个MVHtmlString

    public MvcHtmlString CreateMenu(this HtmlHelper helper, MenuSecurityModel)
    {
       //create tags, string, etc
       //create logic add/remove meun items depending on flag property on model
       //this can be accomplished also by using a base class for your Controllers, 
       //set the the onactionexecuting variable to set the menu[ put it in session accodingly, retrieve when appropriate
       //Call using Html.RenderAction , partial, etc
       return yourHtmlString;
    }
    

    您的另一个选项是创建对HtmlHleper的扩展,然后您可以使用它将模型传递到菜单,并让它生成一个MVHtmlString

    public MvcHtmlString CreateMenu(this HtmlHelper helper, MenuSecurityModel)
    {
       //create tags, string, etc
       //create logic add/remove meun items depending on flag property on model
       //this can be accomplished also by using a base class for your Controllers, 
       //set the the onactionexecuting variable to set the menu[ put it in session accodingly, retrieve when appropriate
       //Call using Html.RenderAction , partial, etc
       return yourHtmlString;
    }
    

    我建议使用视图模型方法来表示菜单项

    public class MenuItem
    {
      public int ID { get; set; }
      public string ControllerName { get; set; }
      public string ActionName { get; set; }
      public string LinkText { get; set; }
      public string ImageUrl { get; set; }
      public bool CanView { get; set; }
    }
    
    @Html.Action("Menu", yourControllerName)
    
    然后,当用户登录时,创建一组菜单项,调用您的方法为用户获取
    安全性
    项,并根据
    标志
    属性,为每个菜单设置
    CanView
    属性。最后将集合存储在
    会话中

    然后创建一个仅用于生成菜单的子方法

    [ChildActionOnly]
    public ActionResult Menu
    {
      IEnumerable<MenuItem> model = Session["Menu"] as IEnumerable<MenuItem>;
      // check for null and rebuild if necessary
      return PartialView("_Menu", model);
    }
    
    最后在布局页面中,使用
    Html.Action()
    调用生成菜单项的方法

    public class MenuItem
    {
      public int ID { get; set; }
      public string ControllerName { get; set; }
      public string ActionName { get; set; }
      public string LinkText { get; set; }
      public string ImageUrl { get; set; }
      public bool CanView { get; set; }
    }
    
    @Html.Action("Menu", yourControllerName)
    

    我建议使用视图模型方法来表示菜单项

    public class MenuItem
    {
      public int ID { get; set; }
      public string ControllerName { get; set; }
      public string ActionName { get; set; }
      public string LinkText { get; set; }
      public string ImageUrl { get; set; }
      public bool CanView { get; set; }
    }
    
    @Html.Action("Menu", yourControllerName)
    
    然后,当用户登录时,创建一组菜单项,调用您的方法为用户获取
    安全性
    项,并根据
    标志
    属性,为每个菜单设置
    CanView
    属性。最后将集合存储在
    会话中

    然后创建一个仅用于生成菜单的子方法

    [ChildActionOnly]
    public ActionResult Menu
    {
      IEnumerable<MenuItem> model = Session["Menu"] as IEnumerable<MenuItem>;
      // check for null and rebuild if necessary
      return PartialView("_Menu", model);
    }
    
    最后在布局页面中,使用
    Html.Action()
    调用生成菜单项的方法

    public class MenuItem
    {
      public int ID { get; set; }
      public string ControllerName { get; set; }
      public string ActionName { get; set; }
      public string LinkText { get; set; }
      public string ImageUrl { get; set; }
      public bool CanView { get; set; }
    }
    
    @Html.Action("Menu", yourControllerName)
    

    我建议使用视图模型方法来表示菜单项

    public class MenuItem
    {
      public int ID { get; set; }
      public string ControllerName { get; set; }
      public string ActionName { get; set; }
      public string LinkText { get; set; }
      public string ImageUrl { get; set; }
      public bool CanView { get; set; }
    }
    
    @Html.Action("Menu", yourControllerName)
    
    然后,当用户登录时,创建一组菜单项,调用您的方法为用户获取
    安全性
    项,并根据
    标志
    属性,为每个菜单设置
    CanView
    属性。最后将集合存储在
    会话中

    然后创建一个仅用于生成菜单的子方法

    [ChildActionOnly]
    public ActionResult Menu
    {
      IEnumerable<MenuItem> model = Session["Menu"] as IEnumerable<MenuItem>;
      // check for null and rebuild if necessary
      return PartialView("_Menu", model);
    }
    
    最后在布局页面中,使用
    Html.Action()
    调用生成菜单项的方法

    public class MenuItem
    {
      public int ID { get; set; }
      public string ControllerName { get; set; }
      public string ActionName { get; set; }
      public string LinkText { get; set; }
      public string ImageUrl { get; set; }
      public bool CanView { get; set; }
    }
    
    @Html.Action("Menu", yourControllerName)
    

    我建议使用视图模型方法来表示菜单项

    public class MenuItem
    {
      public int ID { get; set; }
      public string ControllerName { get; set; }
      public string ActionName { get; set; }
      public string LinkText { get; set; }
      public string ImageUrl { get; set; }
      public bool CanView { get; set; }
    }
    
    @Html.Action("Menu", yourControllerName)
    
    然后,当用户登录时,创建一组菜单项,调用您的方法为用户获取
    安全性
    项,并根据
    标志
    属性,为每个菜单设置
    CanView
    属性。最后将集合存储在
    会话中

    然后创建一个仅用于生成菜单的子方法

    [ChildActionOnly]
    public ActionResult Menu
    {
      IEnumerable<MenuItem> model = Session["Menu"] as IEnumerable<MenuItem>;
      // check for null and rebuild if necessary
      return PartialView("_Menu", model);
    }
    
    最后在布局页面中,使用
    Html.Action()
    调用生成菜单项的方法

    public class MenuItem
    {
      public int ID { get; set; }
      public string ControllerName { get; set; }
      public string ActionName { get; set; }
      public string LinkText { get; set; }
      public string ImageUrl { get; set; }
      public bool CanView { get; set; }
    }
    
    @Html.Action("Menu", yourControllerName)
    


    您当前如何在视图中生成这些?你有一个视图模型来表示菜单项集合吗?@StephenMuecke没有菜单的视图模型。。它只是像在编辑的第1部分中一样硬编码。选项之一是使用视图模型来表示菜单项-包括控制器和动作名称的属性、图像路径等以及
    bool canView
    属性。然后,您可以为每个用户的每个菜单项设置
    canView
    属性,并将集合存储在会话中。其他可能是具有一系列bool属性的视图模型-
    bool canViewMenu1
    bool可以查看menu2
    等,并将其存储在会话中,然后使用if确定要在会话中显示的菜单view@stephenmuecke有简单的例子吗?您当前如何在视图中生成这些?你有一个视图模型来表示菜单项集合吗?@StephenMuecke没有菜单的视图模型。。它只是像在编辑的第1部分中一样硬编码。选项之一是使用视图模型来表示菜单项-包括控制器和动作名称的属性、图像路径等以及
    bool canView
    属性。然后,您可以为每个用户的每个菜单项设置
    canView
    属性,并将集合存储在会话中。其他可能是具有一系列bool属性的视图模型-
    bool canViewMenu1
    bool可以查看menu2
    等,并将其存储在会话中,然后使用if确定要在会话中显示的菜单view@stephenmuecke有简单的例子吗?您当前如何在视图中生成这些?你有一个视图模型来表示菜单项集合吗?@StephenMuecke没有菜单的视图模型。。它只是像在编辑的第1部分中一样硬编码。选项之一是使用视图模型来表示菜单项-包括控制器和动作名称的属性、图像路径等以及
    bool canView
    属性。然后,您可以为每个用户的每个菜单项设置
    canView
    属性,并将集合存储在会话中。其他可能是具有一系列bool属性的视图模型-
    bool canViewMenu1
    bool可以查看menu2
    等,并将其存储在会话中,然后使用if确定要在会话中显示的菜单view@stephenmuecke有简单的例子吗?您当前如何在视图中生成这些?你有一个视图模型来表示菜单项的集合吗?@StephenMuecke没有菜单的视图模型。。它只是像在编辑过的第1部分中一样硬编码。选项之一是使用视图模型来表示你的菜单项