<asp:Menu ID="mnu" runat="server" PathSeparator="," CssClass="menu" DynamicMenuItemStyle-CssClass="menu">
<Items>
<asp:MenuItem Text="home" NavigateUrl="~/Default.aspx" />
<asp:MenuItem Text="Aboutus" NavigateUrl="#"/>
<asp:MenuItem Text="Support" NavigateUrl="#" />
</Items>
</asp:Menu>
I have this menu in master page, When the user logs into the website, based on the user role I want to add items to the menu from the server side. How can I do 开发者_StackOverflowthat.
Admin(menu to add --> Organisation, Message, Group) Users(menu to add --> Message, Group)
Since I have 6 roles I have different menu item for each role. How can this be done
In the Page_Load
of the master you could check whether the user is in some roles and dynamically add values to the menu:
protected void Page_Load(object sender, EventArgs e)
{
if (User.IsInRole("admin"))
{
mnu.Items.Add(new MenuItem
{
Text = "Administer web site",
NavigateUrl = "~/admin.aspx"
});
}
}
I'd generally use a site map and security trimming. Each siteMapNode has a "roles" attribute that indicates which roles are allowed to see the link in the menu. * is used for all roles or you can enter a comma separated list of roles. e.g.
<?xml version="1.0" encoding="utf-8" ?>
<siteMap>
<siteMapNode title="Home" description="Home"
url="~/default.aspx" roles="*" >
</siteMapNode>
<siteMapNode title="Organization" description="Organization"
url="~/Organization.aspx" roles="Admin" >
</siteMapNode>
<siteMapNode title="Message" description="Message"
url="~/Organization.aspx" roles="Admin, User" >
</siteMapNode>
</siteMap>
etc.
Then you can enable security trimming in your web.config:
<siteMap defaultProvider="XmlSiteMapProvider" enabled="true">
<providers>
<add name="XmlSiteMapProvider"
description="Default Site Map Provider"
type="System.Web.XmlSiteMapProvider"
siteMapFile="Web.sitemap"
securityTrimmingEnabled="true" />
</providers>
</siteMap>
All you have to do then is set the datasource of your asp menu to the site map. More information can be found here: http://msdn.microsoft.com/en-us/library/305w735z.aspx and here: http://msdn.microsoft.com/en-us/library/ms178429(v=vs.80).aspx
I like this approach because adding a new role based menu item is much easier. You don't have to manually check the role in the code behind which will probably end up as an unwieldy if statement anyway.
You can create a Session when the user login.
Session["user"] = user;
In Master Page;
var user = Session["user"];
Dynamic User Privilege Based Menu
C# CLASS FILES
public class url_details
{
public string url;
public string page_name;
public string icon;
}
C# inside login page
List<url_details> url_list = new List<url_details>();
foreach (DataRow dr in dataTable.Rows)
{
url_details url_item = new url_details();
url_item.url = dr["url"].ToString();
url_item.page_name = dr["page_name"].ToString();
url_item.icon = dr["icon"].ToString();
url_list.Add(url_item);
}
Session["urls"] = url_list;
C#-HTML MENU FORM
<%
var uruls = (List<url_details>)Session["urls"];
foreach (var url in uruls)
{%>
<li><a href="..<%=url.url %>"><%=url.icon %><span><%=url.page_name %></span></a></li>
<% }
%>
精彩评论