在菜单之间应用安全性 [英] applying security among menus
问题描述
下面是我的模特
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
,则用户可以访问子菜单.
it the flag is true
the user has access to submenu.
目前我正在做的是.当用户登录时,该模型中的详细信息将被拉出并存储在单独的会话变量中.
Currently what i am doing is . When the user logged in the details in this model will be pulled out and store in seperate session variable.
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 sub menus
,以后可以增加它.因此,我必须为每个子菜单创建单独的会话变量.有没有更好的方法来解决这个问题?
Then in the layout view will check this session variable and if true
the menu will display, but problem is there are 16 sub menus
and can increase it later.So I have to create seperate session variables for each submenu. Is there any better way to solve this problem?
已编辑
@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>
}
推荐答案
我建议使用一种视图模型方法来表示您的菜单项
I would suggest a view model approach to represent your menu items
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; }
}
然后,当用户登录时,创建菜单项的集合,调用您的方法为用户获取security
项,并基于flag
属性,为每个菜单设置CanView
属性.最后将集合存储在Session
Then when a user logs in, create a collection of menu items, call your method to get the security
items for the user, and based on the flag
property, set the CanView
property for each menu. Finally store the collection in Session
然后创建一个只允许孩子使用的方法来生成菜单
Then create a child only method to generate your menu
[ChildActionOnly]
public ActionResult Menu
{
IEnumerable<MenuItem> model = Session["Menu"] as IEnumerable<MenuItem>;
// check for null and rebuild if necessary
return PartialView("_Menu", model);
}
并创建局部视图_Menu.cshtml
@model IEnumerable<yourAssembly.MenuItem>
<ul>
@foreach(var menu in Model)
{
if (menu.CanView)
{
<li>
<a href=""@Url.Action(menu.ActionName, menu.ControllerName)">
<img src="@Url.Content(menu.ImagePath)" alt="">
<span class="title"><b>menu.LinkText</b></span>
</a>
</li>
}
}
</ul>
最后在布局页面中,使用Html.Action()
调用生成菜单项的方法
and finally in the layout page, use Html.Action()
to call the method that generates the menu items
@Html.Action("Menu", yourControllerName)
这篇关于在菜单之间应用安全性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!