C# 如何根据asp.net中的角色隐藏菜单项
我有如下Web.sitemap文件:C# 如何根据asp.net中的角色隐藏菜单项,c#,asp.net,menu,C#,Asp.net,Menu,我有如下Web.sitemap文件: <?xml version="1.0" encoding="utf-8" ?> <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" > <siteMapNode url="" title="" description=""> <siteMapNode url="~/Home.aspx" title="H
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="" title="" description="">
<siteMapNode url="~/Home.aspx" title="Home" description=" this is the home page" />
<siteMapNode url="~/ProjectList.aspx" title="Project List" description="Approved projects" />
<siteMapNode url="" title="Project Choices" description="">
<siteMapNode url="~/StudentChoices.aspx" title="Student Project Choices" description="" />
<siteMapNode url="~/StaffChoices.aspx" title="Supervisor Project Choices" description="" />
</siteMapNode>
<siteMapNode url="~/AllocationList.aspx" title="Project Allocation List" description="" />
<siteMapNode url="" title="Submit Proposal" description="" >
<siteMapNode url="~/submit.aspx" title="New Proposal" description="new proposal" />
<siteMapNode url="~/reSubmit.aspx" title="Re-Submit Proposal" description="re submit proposal"/>
</siteMapNode>
<siteMapNode url="~/StaffRecords.aspx" title="Staff Records" description="" >
<siteMapNode url="~/addStaff.aspx" title="Add new Staff" description="" />
</siteMapNode>
<siteMapNode url="~/StudentRecords.aspx" title="Student Records" description="" />
<siteMapNode url="~/Administration.aspx" title="Administration" description="" />
</siteMapNode>
</siteMap>
和函数管理器中的UItemarroles
public void ManageMenuItemAsperRoles()
{
string role = Session["Roles"].ToString();
string AdminRole = ConfigurationManager.AppSettings["AdminRole"];
string StaffRole = ConfigurationManager.AppSettings["StaffRole"];
string StudentRole = ConfigurationManager.AppSettings["StudentRole"];
if (role == StaffRole)
{
MenuItemCollection menuItems = Menu1.Items;
MenuItem ProjectChoicesItem = new MenuItem();
MenuItem StaffRecordsItem = new MenuItem();
MenuItem StudentRecordsItem = new MenuItem();
foreach (MenuItem menuItem in menuItems)
{
if (menuItem.Text == "Project Choices")
ProjectChoicesItem = menuItem;
}
foreach (MenuItem menuItem in menuItems)
{
if (menuItem.Text == "Staff Records")
StaffRecordsItem = menuItem;
}
foreach (MenuItem menuItem in menuItems)
{
if (menuItem.Text == "Student Records")
StudentRecordsItem = menuItem;
}
menuItems.Remove(ProjectChoicesItem);
menuItems.Remove(StaffRecordsItem);
menuItems.Remove(StudentRecordsItem);
}
}
但问题是,当我放置断点时,我发现在:Menu1.Items
我无法删除一些菜单项
为什么?不,不在页面加载中。您必须在MenuItemDataBound中执行此操作:
protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs e)
{
string role = Session["Roles"].ToString();
string AdminRole = ConfigurationManager.AppSettings["AdminRole"];
string StaffRole = ConfigurationManager.AppSettings["StaffRole"];
string StudentRole = ConfigurationManager.AppSettings["StudentRole"];
if (role == StaffRole)
{
if (e.Item.Text == "Project Choices" ||
e.Item.Text == "Staff Records" ||
e.Item.Text == "Student Records")
{
Menu1.Items.Remove(e.Item);
}
}
}
.Net Web不为每个菜单项提供属性以指定可见性。因此,无法将特定菜单项设置为每个用户都可见。但是,需要注意的是,您可以不首先创建菜单项 例如:不要通过VisualStudioDesigner视图添加菜单项。添加静态的项,而不考虑身份验证级别 在on Page Load事件中-执行身份验证检查。如果用户已通过身份验证,则通过面向对象的方法(利用其构造函数并设置任何所需的属性)创建要在身份验证时显示的菜单项。母版页:因为我希望所有页面的菜单都是这样 但是,如果需要的话,您可以在每个页面上进行
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
MenuItem m = new MenuItem("Upload");
m.NavigateUrl = "~/Uploader/Upload.aspx";
NavigationMenu.Items.Add(m);
}
}
PS-我已经在类似问题的其他地方发布了这个答案,但是这个答案对于这个场景仍然有效。
为了进一步增强这一点,您可以分析HttpContext.Current.User.Identity类,以获取用户的用户名,然后专门为用户显示菜单选项。
但是,用户角色的身份验证不应依赖用户名本身。使用来自数据库的UID,并将非用户友好的令牌分配给用户作为其身份,这将比标识其帐户的字符串变量更安全。
但现在讨论的是一个不同的主题:上面关于MenuItem的详细信息应该足够了。不要在menuItems上迭代3次,而应该在迭代过程中执行一次并检查每个条件。你的程序在那里做了很多不必要的循环。但这并不能解决你的问题。我不担心不必要的循环,我知道这一点,但主要目标是删除项目
protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs e)
{
string role = Session["Roles"].ToString();
string AdminRole = ConfigurationManager.AppSettings["AdminRole"];
string StaffRole = ConfigurationManager.AppSettings["StaffRole"];
string StudentRole = ConfigurationManager.AppSettings["StudentRole"];
if (role == StaffRole)
{
if (e.Item.Text == "Project Choices" ||
e.Item.Text == "Staff Records" ||
e.Item.Text == "Student Records")
{
Menu1.Items.Remove(e.Item);
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
MenuItem m = new MenuItem("Upload");
m.NavigateUrl = "~/Uploader/Upload.aspx";
NavigationMenu.Items.Add(m);
}
}