【问题标题】:How to get different menu items showing for admins vs users如何为管理员和用户显示不同的菜单项
【发布时间】:2013-06-25 22:32:48
【问题描述】:

我正在尝试设置我的网站,其中所有链接都显示为管理员角色,并且显示有限数量的用户角色。

这是我的站点地图:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode >
    <siteMapNode url="fake1.aspx" title="Example A"  description="Example" roles="user">
      <siteMapNode url="fake2.aspx" title="Example 1"  description="Example" roles="user" />
      <siteMapNode url="fake3.aspx" title="Example 2"  description="Example" roles="user" />
      <siteMapNode url="fake4.aspx" title="Example 3"  description="Example" roles="user" />
    </siteMapNode>
    <siteMapNode url="fake5.aspx" title="Example B"  description="Example" roles="user">
      <siteMapNode url="fake6.aspx" title="Example 1"  description="Example" roles="user" />
      <siteMapNode url="fake7.aspx" title="Example 2"  description="Example" roles="user" />
      <siteMapNode url="fake8.aspx" title="Example 3"  description="Example" roles="user" />
    </siteMapNode>
    <siteMapNode url="fake9.aspx" title="Example C"  description="Example" roles="user">
      <siteMapNode url="fake10.aspx" title="Example 1"  description="Example" roles="user" />
      <siteMapNode url="fake11.aspx" title="Example 2"  description="Example" roles="user" />
      <siteMapNode url="fake12.aspx" title="Example 3"  description="Example" roles="user"/>
    </siteMapNode>
    <siteMapNode url="fake13.aspx" title="Example D"  description="Example" roles="admin">
      <siteMapNode url="fake14.aspx" title="Example 1"  description="Example" roles="admin" />
      <siteMapNode url="fake15.aspx" title="Example 2"  description="Example" roles="admin" />
      <siteMapNode url="fake16.aspx" title="Example 3"  description="Example" roles="admin" />
    </siteMapNode>
  </siteMapNode>
</siteMap>

这是我的菜单服务器控件:

<div id="menuBar">
    <asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1" StaticDisplayLevels="2"
        Orientation="Horizontal" >
    </asp:Menu>
    <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
</div>

这是我的角色截图:

从我一直在阅读的教程中,roles="" 应该根据每个角色来处理链接访问,但我的网站上没有发生任何事情。用户和管理员看到的是同样的事情。我做错了什么?

【问题讨论】:

    标签: c# asp.net xml visual-studio roles


    【解决方案1】:

    在显示管理员相关内容之前检查用户是否处于正确的角色:-

    if (Membership.GetUser() != null && Page.User.IsInRole("admin"))
    {
        //User is an admin
    }
    

    【讨论】:

    • 我没有通过 c# 显示菜单,我可以使用 c# 隐藏或显示部分站点地图吗?
    • 您可以根据角色换出数据源或在将其分配给菜单之前对其进行过滤
    猜你喜欢
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多