微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 使用隐藏的Sitemap节点来控制访问

在我的Asp.NET C#应用程序中,我有一个站点地图绑定到网站导航菜单.为了控制用户访问,我使用 NHibernate Custom Role Provider by Manuel Abadia,并在每个SiteMap节点上设置Roles属性.

<siteMapNode title="Add/Modify Roles" description="Add/Modify User Roles"
url="~/MemberPages/Admin_Roles.aspx" roles="Administrator" />

到目前为止,一切都运行良好,但现在我希望有几个网页没有出现在站点地图绑定菜单上,但仍然使用站点地图节点来控制用户访问.例如,我有一个专门用于管理员页面,它显示数据库中的所有角色(ViewPage).在该页面上,我有一个添加新”按钮,用户用户重定向到另一个页面添加新角色(AddPage). ViewPage应该存在于菜单上,而AddPage不应该存在,但两者都应该共享相同的访问设置(只能由管理员访问).

我该如何实现这一目标?我知道我可以在我的web.config文件中声明访问设置,但我宁愿避免这种情况,因为会有很多像这样的页面.此外,系统管理员将被允许自定义访问规则,例如将其从仅管理员更改为管理员管理员.主持人,如果系统可以改变一个站点地图中所有页面的访问规则,将会更方便.

解决方法

感谢 MikeSmithDev’s comment,我设法找到了一个很好的解决方案.如果其他人遇到同样的问题,这就是我做的:

在我的SiteMap中,我添加一个自定义属性HideFromMenu:

<siteMapNode title="Add Page" description="Page to Add stuff"
roles="Administrator" url="~/AddPage.aspx" HideFromMenu="true" />

在我的Menu控件中,我添加了OnMenuItemDataBound属性

<asp:Menu ID="Menu1" runat="server" DataSourceID="siteMapSource" 
OnMenuItemDataBound="Menu1_MenuItemDataBound" >

而在主人的代码隐藏中:

protected void Menu1_MenuItemDataBound(object sender,MenuEventArgs e)
{
    SiteMapNode node = (SiteMapNode)e.Item.DataItem;
    if (node["HideFromMenu"] == "true")
    {
        if (e.Item.Parent != null) //if this item has a parent..
            e.Item.Parent.ChildItems.Remove(e.Item); //use parent to remove child..
        else
            Menu1.Items.Remove(e.Item); //else.. remove from menu itself.
    }
}

最后,为了确保用户不访问离线页面,我在主页面添加了一些验证.

var authorizedRoles = SiteMap.CurrentNode.Roles; //obtain the list of authorized roles for the current page

if (authorizedRoles == null || authorizedRoles.Count == 0) // no roles defined for this node
    isAuthorized = false;
else
{
    if (authorizedRoles.Contains("*")) // this page can be accessed by everyone
        isAuthorized = true;

    if (!isAuthorized)
    {
        foreach (string authorizedRoleName in authorizedRoles)
        {
            if (Roles.IsUserInRole(authorizedRoleName) == true)
            {
                isAuthorized = true;
                break;
            }
        }
    }
}

if (!isAuthorized)
    Response.End(); // unauthorised user; kill the page.

我希望这可以帮助那些面临同样问题的人.如果有更好的方法可以达到相同的结果,请告诉我.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐