1

我有

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>

但我仍然能够看到这些菜单,我无法找出为什么它没有被删除,谁能告诉我为什么?给我看示例代码。

4

2 回答 2

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>
于 2013-09-11T13:29:19.523 回答
0

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

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

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

有一个Security trimming结合ASP.NET Membership可以管理基于用户角色的访问。

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

于 2013-09-11T13:07:12.450 回答