无法根据asp.net中的角色删除菜单项

时间:2013-09-11 11:41:46

标签: c# asp.net asp.net-mvc

我有

像这样的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="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>

我使用此站点地图创建菜单项:如下所示:

<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal"
                BackColor="#33CCFF" Font-Overline="False" DataSourceID="SiteMapDataSource1"
                Font-Size="Larger" ForeColor="Black" ItemWrap="True" StaticDisplayLevels="2"
                StaticSubMenuIndent="60px" Width="100%" OnMenuItemDataBound="Menu1_MenuItemDataBound">
                <DynamicHoverStyle BackColor="#9999FF" ForeColor="Black" />
                <DynamicMenuItemStyle BackColor="#0099FF" ForeColor="Black" />
                <DynamicMenuStyle BackColor="#0099FF" />
                <DynamicSelectedStyle BackColor="#0099FF" ForeColor="Black" />
            </asp:Menu>


<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
代码隐藏

我写了以下内容:

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 (StaffRole == "Staff")
        {
            if (e.Item.Text == "Project Choices" ||
                e.Item.Text == "Staff Records" ||
                e.Item.Text == "Student Records")
            {
                Menu1.Items.Remove(e.Item);
            }
        }
    }

和像这样的网络配置

<appSettings>
        <add key="AdminRole" value="Admin"/>
        <add key="StaffRole" value="Staff"/>
        <add key="StudentRole" value="Student"/>
    </appSettings>

但是我仍然可以看到那些菜单,我无法找出它为什么不能删除,有人可以告诉我为什么吗?给我看示例代码。

2 个答案:

答案 0 :(得分:0)

siteMapNode已经提供了一个名为roles的属性。此属性包含可以访问该网站区域的roles

例如,项目列表应如下所示:

<siteMapNode url="~/ProjectList.aspx" 
 title="Project List"  
 description="Approved projects" 
 roles="AdminRole"/>

Theres Security trimmingASP.NET Membership一起可以根据用户角色管理访问权限。

你应该看看这里:ASP.NET Site-Map Security Trimming

答案 1 :(得分:0)

 The easiest way  to use a SiteMapProvider is  to configure with SecurityTrimmingEnabled = true.

 Add this under Web.Config file 
<siteMap enabled="true" defaultProvider ="AspNetXmlSiteMapProvider" >
  <providers>
    <clear/>
    <add siteMapFile="Web.sitemap" name="AspNetXmlSiteMapProvider" type="System.Web.XmlSiteMapProvider" securityTrimmingEnabled="true" />
  </providers>
</siteMap>

try to add the roles to the site map like this
<siteMapNode url="~/StaffRecords.aspx" title="Staff Records"  description=""     roles="StaffRole">
  <siteMapNode url="~/addStaff.aspx" title="Add new Staff" description="" />
</siteMapNode>