I'm working on a web site where I have a requirement to show menu in the master page based on the user roles like Admin, manager, guest etc. What is the best way to accomplish this in ASP.NET 3.5 ?
Well the way I did it was using sessions in order to validate the person on login to determine what role they are in from a SQL database. The session would store the query result for there user role such as admin, manager and guest.
Depending on the Session["role"] = QueryRoleResult (based on the user logging in could be Admin, manager, guest) was stored here I would generate the master page UI using the aspx.cs behind the master page file for navigation.
IE :
switch (Session["role"].ToString)
case "admin":
navfunctionAdminUI();
break;
case "guest":
navfunctionGuestUI();
break
etc...
etc...
I am assuming you have a login page with usernames and passwords stored on a database of some sort. In my case it was a Microsoft SQL server which i accessed to retrieve there role.
This may not be the best way to do it, but it worked for me. Depending on the person logging into the site, once I validated them I would query there role and store that information in a session and simply use that to determine what the UI should be for
the masterPage navbar.
What you probably need to know how to do:
Dynamically generate controls and assign events to them when clicked
Create and understand session and session states
How to make a connection to a database through a connection string and retrieve a query
You can use asp.net membership provider to provide the role based authentaction. On the page load of the master page, check the role and depend on that, set the Visible property of the asp:Menu
to true/false.
Is Master page a the best place to contain the menu ?
Hi Vinod,
Well, it actually depends on your application. If you need the menu to be consistent across content pages, then you can include the menu in the master page. If you need the menu to be displayed only for a particular content page, then there is no point of
having the menu in the master page. Then just include the menu only in that content page.
As Ruchira said it depends on what you want and what you need for your application. If you want the same navbar across all content pages, building the navMenu in the masterpage is worth looking into.
In order to fully understand how masterpages work MSDN has a nice article that is worth reading.
I highly, and I mean highly recommend reading some of those articles. I actually learned more about ASP.net's framework by simply reading and following MSDN articles and tutorials than I did through any other method.
vinod.davis
Member
1 Points
3 Posts
How to build context based menu in the master page ?
Jul 06, 2012 03:46 PM|LINK
I'm working on a web site where I have a requirement to show menu in the master page based on the user roles like Admin, manager, guest etc. What is the best way to accomplish this in ASP.NET 3.5 ?
Regards,
Vinod.
afire007
Member
17 Points
74 Posts
Re: How to build context based menu in the master page ?
Jul 06, 2012 04:37 PM|LINK
Well the way I did it was using sessions in order to validate the person on login to determine what role they are in from a SQL database. The session would store the query result for there user role such as admin, manager and guest.
Depending on the Session["role"] = QueryRoleResult (based on the user logging in could be Admin, manager, guest) was stored here I would generate the master page UI using the aspx.cs behind the master page file for navigation.
IE :
I am assuming you have a login page with usernames and passwords stored on a database of some sort. In my case it was a Microsoft SQL server which i accessed to retrieve there role.
This may not be the best way to do it, but it worked for me. Depending on the person logging into the site, once I validated them I would query there role and store that information in a session and simply use that to determine what the UI should be for the masterPage navbar.
What you probably need to know how to do:
Dynamically generate controls and assign events to them when clicked
Create and understand session and session states
How to make a connection to a database through a connection string and retrieve a query
Ruchira
All-Star
42887 Points
7020 Posts
MVP
Re: How to build context based menu in the master page ?
Jul 07, 2012 11:57 AM|LINK
Hello,
You can use asp.net membership provider to provide the role based authentaction. On the page load of the master page, check the role and depend on that, set the Visible property of the asp:Menu to true/false.
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.vinod.davis
Member
1 Points
3 Posts
Re: How to build context based menu in the master page ?
Jul 09, 2012 01:07 PM|LINK
Hi,
Is Master page a the best place to contain the menu ?
Regards,
Vinod.
Ruchira
All-Star
42887 Points
7020 Posts
MVP
Re: How to build context based menu in the master page ?
Jul 09, 2012 01:21 PM|LINK
Hi Vinod,
Well, it actually depends on your application. If you need the menu to be consistent across content pages, then you can include the menu in the master page. If you need the menu to be displayed only for a particular content page, then there is no point of having the menu in the master page. Then just include the menu only in that content page.
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.afire007
Member
17 Points
74 Posts
Re: How to build context based menu in the master page ?
Jul 09, 2012 02:12 PM|LINK
As Ruchira said it depends on what you want and what you need for your application. If you want the same navbar across all content pages, building the navMenu in the masterpage is worth looking into.
In order to fully understand how masterpages work MSDN has a nice article that is worth reading.
http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx
I highly, and I mean highly recommend reading some of those articles. I actually learned more about ASP.net's framework by simply reading and following MSDN articles and tutorials than I did through any other method.