[RESOLVED] Show menu item according to user's rights
Hi,
I'm a newbie in Web applications and I'm experimenting with Master pages in ASP.NET 2.0 with VS2005. I've created a menu in master page that wiil be used throught the application. I want to show menu Items according to logged user's profile. If he is admin then all menu item and if other than admin then according to given rights. Is this possible, if yes then how??
Please guide me,
Thanks in Advance
ASP.NET 2.0 has a menu control, which you can point at an XML file. Create a separate XML file for each of the 'ranks' among your members. In the Master Page, check for the value of the session variable, which should contain the rank. Or whatever your mechanism is. Then according to the rank, point it at the corresponding XML file.
The fact that you are using ASP.NET 2.0 makes this very easy!!
I am going to assume that you have used the built in User/Role Manager within VS 2005, and that you have set up an Administrators Role within the Website | ASP.NET Configuration Section.
Having done that, simply drag and drop a Menu from the Navigation Sub Menu in the Toolbox onto the form, and also a SiteMapDataSource. You should also put on a login control (maybe put this in a LoginView) and maybe a LoginStatus as well.
Point the datasource of the Menu Control at the SiteMapDataSource and then add a sitemap to your project by right clicking on the project, add new item, SiteMap and fill it with something like the following:
Next, add a folder to the project and call is Admin, and then create the a Page within this folder called Test1.aspx, and add another page to the route of the Project called Test2.aspx.
Having done that, you then need to add the following to your web.config file:
Now, with all of that done, when you run the website, you should see that initially there is only one entry in the Menu, called Test2, but when you log in with someone who is a member of Administrators, then you will see two entries and you will be able to navigate to that page.
It seems like quite a lot of work, but once you have done it a couple of times, you will soon get the hang of it!!
Thanks gep13 for your valuable information, but I am confused now,
I created three category for login in my database, and all three logins have different rights.I've created three diff. xml files but don't know how to bind these xml files to my menu control. Should I have a dataset and fill according to category and then bind to Menu or anything else.
Please guide me
Thanks again
Bye
I think there has been some confusion between the methods that we are employing.
Are you using the built in user access database and login controls that ship with VS 2005, or are you using your own way of authenticating to the website?
Thanks gep13,
yes sir, I am using access database where I've assigned diff. rights for diff. users. I am not using login controls.
Its not a web site my application, its an intranet app where few users can access it and each user has an ID n Password.
Its a simple application. Hope you get it.
As I told in my first post that I am very new to It. So Iam very confused
bytheway thanks and hoping some solution fromyou.
The method that I described will only work if you are using the built in controls and user providers that ship with VS 2005. Since you are not using this approach then I am not really going to be able to help you as I have not really played with what you are describing.
Here's how to do it with an XML file, from codeguru.com
Code:
private void CreateMenuWithXmlFile()
{
string path = @"C:\MyXmlFile.xml";
DataSet ds = new DataSet();
ds.ReadXml(path);
Menu menu = new Menu();
menu.MenuItemClick += new MenuEventHandler(menu_MenuItemClick);
for (int i = 0; i < ds.Tables.Count; i++)
{
MenuItem parentItem = new MenuItem((string)ds.Tables[i].TableName);
menu.Items.Add(parentItem);
for (int c = 0; c < ds.Tables[i].Columns.Count; c++)
{
MenuItem column = new MenuItem((string)ds.Tables[i].Columns[c].ColumnName);
menu.Items.Add(column);
for (int r = 0; r < ds.Tables[i].Rows.Count; r++)
{
MenuItem row = new MenuItem((string)ds.Tables[i].Rows[r][c].ToString());
parentItem.ChildItems.Add(row);
}
}
}
Panel1.Controls.Add(menu);
Panel1.DataBind();
}
Now, all you need to do, is create three separate XML files, one for each of your roles. More if there are more roles. Based upon the role of the user who has logged in, load the corresponding XML file and follow the same procedure.
A more efficient way would be to store all your menu items in the database itself, and each menu item corresponds to a role. So for example, Role A might have item1,item4,item5,item6, while Role B might have item1,item2,item3, item 5. You will load the dataset based upon the role from the database, and then, here's more code from codeproject.
Code:
private void PopulateMenu()
{
DataSet ds = GetDataSetForMenu();
Menu menu = new Menu();
foreach (DataRow parentItem in ds.Tables["Categories"].Rows)
{
MenuItem categoryItem = new MenuItem((string)parentItem["CategoryName"]);
menu.Items.Add(categoryItem);
foreach (DataRow childItem in parentItem.GetChildRows("Children"))
{
MenuItem childrenItem = new MenuItem((string)childItem["ProductName"]);
categoryItem.ChildItems.Add(childrenItem);
}
}
Panel1.Controls.Add(menu);
Panel1.DataBind();
}
Thanks Mendhak
I tried your method but ds.Tables.Count is 0 so nothing is happening,
Probably something is wrong with xml file using your method.
I tried other option also that is Web.siteMap and it is working fine( in case of single user) but I think this Web.sitemap must be only one in our project. So what would happen if multiple users will access my application.
Actually I m creating Web.sitemp after user's login and I 've diferent xml files for those profile, I just copied that xml file to Web.sitemp and its working. So
everytime user logins, the Web.sitemp is updated. So I am worry about mutiple users case.
Can you help me out. Thanks again
Bye
Last edited by Bajrang; Apr 24th, 2007 at 05:26 AM.
Unless i am missing something, which I more than likely am, then I think you are over complicating this.
The way I see this is, you would have one web.sitemap, and this would detail all the available menu items, i.e. the entire hierarchy. Then using the web.config file, you would configure access to these pages/folders as shown earlier. This may involve having multiple roles, and multiple users can belong to multiple roles.
Once this is set up, then the menu items will change dynamically, based on who is logged in at any time.
Thanks Gary,
As I've not tested it on multiple machines, so I am afraid.
But as I run different Instances of application for different user profile on the same machine , its working fine and Web.sitemap is changed according to logged user(Last one).I checked it, web.sitemap is modified according to last looged user.
I think once menu is bind to web.sitemap , it doesn't affected even if web.sitemap is changed for that specific user. Am I Correct?
Thanks Again
Last edited by Bajrang; Apr 24th, 2007 at 07:36 AM.
Again, I am not sure I fully understand what it is exactly you are doing. I have only ever used 1 web.sitemap, and this has worked well in all the situations I have used it.
I can't say for definite what happens if you then start adding "new" web.sitemap for different users. I still don't understand why you need to load different site maps, can you give an example of the different site maps you are trying to use?
example of the different site maps.
I choose one of three xml files according to logged users(Operator, mgmtstaff,storekeeper) then I copy all contents of that xml file to Web.sitemap file . Now, I think, you will get it. Have a look and suggest me what improvement is possibe there. Thanks
So, why can you not combine all three files into one sitemap?
You obviously have three roles:
Operator
Manager
StoreKeeper
Operator have access to:
Entry
Update
View
Management have access to:
View
StoreKeeper have access to:
Entry
View
So the way i would do this is to store all the Entry files in a folder called entry, all the Update files in one called update, and all the view files in a folder called view. Then in the web.config files, restrict access to these folders. i.e.
Unless I am really missing something, i don't see why this would be a problem. All you need to do is to create the correct roles within your application, and then add users into the roles which they are a member of.
Hope this helps!
Last edited by gep13; Apr 24th, 2007 at 08:14 AM.
Reason: roles were wrong
Thanks a lot.
AS you told I created a single xlm file that has all rights.
So the way i would do this is to store all the Entry files in a folder called entry, all the Update files in one called update, and all the view files in a folder called view. Then in the web.config files, restrict access to these folders
As I told I am newbie in ASP.NET, I didn't get what you have done in AddEditPost.aspx, so please suggest how can change my web.config file for these users.Thanks Again
Have a look on this file IS it correct according to you?
Now in terms of what you need to do within the web.config file...
I will assume that following:
1. You have created three folders within the root of your application, entry, view and update
2. You have assigned three roles to your application, Operator, Manager, StoreKeeper
Your web.config needs to be modified to include following (as per post #5 above):
Thanks a lot, You've done a lot of work for me.
Excellent!. I understand now,but I still have one problem that what should I do those folder.Should I put those different xml files for diff. users? If yes then what should be name of files.
If not then please let me know.
Thanks a lot
Please find attached a very basic application which shows you the menu action that you requested (well at least my interpretation of it!! )
I found a slight problem with the sitemap that you had, in that you need to have a url for each sitemapNode, otherwise the menu does not work properly.
To start with, only the "Home" Menu item is visible because no one is logged in.
You can log into the site using the following username and password combinations:
UserName Password
manage manage1#
operate operate1#
store store1#
As you log in which each different user, you should see that the menu items that are available change, and also that users are not allowed to browse directly to the link either, but rather they just come back to the same page and are prompted for a user name and password of a user who has access to that page.
Hope all of it makes sense, if not feel free to post any questions.
Re: [RESOLVED] Show menu item according to user's rights
Hi,
One more question How can I insert Seprator between menu items using web.sitemap. and also how can align Menu item to left, currently they are central aligned.Thanks Again for your valuable information,
Have a good Day,
Bye
Last edited by Bajrang; Apr 27th, 2007 at 01:51 AM.