I needed my treeview only to show a link if a user was in a role that could view that page.
Easy, SecurityTrimming, you may think. However, I use the same ASP.NET page to cover several links in my treeview, some of which may be viewable to the user and some of which may not. SecurityTrimming (if I understand it correctly) would mean I would have to either grant access to the ASP.NET page or deny it; no inbetween.
Fortunately I found and hacked (it didn't work for me without a few changes) some code that removes a link if a user is not in an appropriate role. When a treeview node is databound the sitemapnode is checked for roles and matched to the users roles.
protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
{
SiteMapNode myNode = (SiteMapNode)e.Node.DataItem;
foreach (string myRole in myNode.Roles)
{
if (!Roles.IsUserInRole(myRole.Trim()) && Roles.RoleExists(myRole.Trim()))
{
e.Node.Parent.ChildNodes.Remove(e.Node);
}
}
}
No comments:
Post a Comment