Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 17th, 2006, 10:43 AM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default What is best method to solve problem

This post is related to my previous post (7/16/06). I am asking for guidance rather than code. Here's the problem: I have a masterpage with a complicated menu based on the project's web.sitemap (xml file). As each user logs on and proceeds with the app, I need to enable/make visible certain menu choices and disable/make invisible others on every single page that visually inherited from the masterpage. There are about 60 context pages, all of which inherit from a single masterpage.

Users can login with any one of a number of roles, but once logged on their role is fixed and so is the menu scheme for that user session. If a user wants to login with a different role, he has to logout then relogin and then the menu choices are fixed for the new role/session.

I am undecided about which is the best way to accomplish this. Rather than spend days (weeks probably) developing alternatives, all for the first time, I'd like to be told how to proceed from someone who has done this or something similar. (This must be a widespread need -- seems to me).

If possible I would like to write a class which handles the settings (enable, disable, visible, not visible,...) menu choices to encapsulate the functionality. Then from some page handler, preinit or load, instantiate the class, do the menu fixing and get rid of the class but keeping the adjusted menu.

And really hopefully, I'd like to change the *masterpage* menu that is instantiated on each of the context pages, so that I only have to mess with the menu once, for each user session.

Anyone?

VV



Reply With Quote
  #2 (permalink)  
Old July 18th, 2006, 04:16 AM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi VictorVictor,

I saw your previous post, but didn't have the time to answer it before...

Anyway, this sounds like a good candidate for a user control. Here's what I would do:

1. Create a new User Control (UC) and add a TreeView control together with an XmlDataSource that points to your web.sitemap file.

2. Use the sitemap's Roles nodes to define what roles can see what nodes. E.g.
Code:
<siteMapNode url="~/" title="Home"  description="Administrators">
3. Hook into the TreeNodeDataBound event that fires for each node that is about to be added to the TreeView. Inside that event, you can get a reference to the Roles attribute of the node that is about to be added. Something like this should work:
Code:
protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
{
  XmlElement myElement = (XmlElement)e.Node.DataItem;
  string myRoles = myElement.Attributes["roles"].Value;
  string[] userRoles = myRoles.Split(Convert.ToChar(","));
  foreach (string myRole in userRoles)
  {
    if (!Roles.IsUserInRole(myRole.Trim()) && myRole.Trim() != "*")
    {
      e.Node.Parent.ChildNodes.Remove(e.Node);
    }
  }
}
This C# code extracts the roles from the node, and then sees if the user is in one of the roles. If the user isn't in the role, the node is removed from the tree. You need to modify this code when you want to work with multiple roles on the Roles attribute. Right now, when the user is not in all roles, the node is removed.

4. With all this code and functionality encapsulated in a single UC, all you have to do now is drop this UC on your master page, so all pages can display it.

You can make the UC a bit smarter by implementing properties and/or events so calling pages can see what items are currently display in the tree.

I used a TreeView as an example, but there are other ways to accomplish this as well.

Does this help?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Reply With Quote
  #3 (permalink)  
Old July 18th, 2006, 04:23 AM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar:
Thanks for the reply. I have gotten up VERY early, for where I am and just happened to be sitting here contemplating this problem and your reply was posted. Good timing!
It will take me a while to digest what you have written and convert it into VB, but thats a can do . Thanks and I'll post results. Regards, VV


Reply With Quote
  #4 (permalink)  
Old July 18th, 2006, 04:29 AM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

I copied this C# code from a UC on my own site. I have a number of Photo albums posted on my site that are only accessible for people in certain roles. I manage an XML file (I am not using the web.sitemap but a separate XML file) and read it in to build up the TreeView. Since my site is (mostly) written in C#, I posted a C# code example, but indeed it shouldn't be too hard to convert it. On your request, I mainly tried to communicate the general idea, without a focus on code...

Good luck with this....

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Reply With Quote
  #5 (permalink)  
Old July 20th, 2006, 01:48 PM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar: I had gotten half way thru coding up a treeview in a UC as you suggested when I happened to run across a help segment that solves the problem without coding! Microsoft has provided a mechanism for just this situation. The fix is to employ what they call
security trimming. All one has to do is turn it on in the web.config, assuming one has implemented ROLES in their project. I list below the lines I had to add to the web.config for this. I've tested this backwards and forwards and it works perfectly.

Also listed below in the web.config is a <location> block. There must be one such block for every aspx page in a website.

So, Thanks Imar for the help. I can see that the UC would have worked just as well.

        . . .
        <siteMap defaultProvider="XmlSiteMapProvider" enabled="true">
            <providers>
                <add name="XmlSiteMapProvider"
                            description="Default SiteMap provider."
                            type="System.Web.XmlSiteMapProvider "
                            siteMapFile="Web.sitemap"
                            securityTrimmingEnabled="true"/>
            </providers>
        </siteMap>
    </system.web>
      <location path="Activity.aspx">
        <system.web>
            <authorization>
                <allow roles="Owner,Chapter Master,Activity Leader,Member,Anonymous"/>
                        <deny users="*" />
            </authorization>
        </system.web>
    </location>



Reply With Quote
  #6 (permalink)  
Old July 20th, 2006, 01:53 PM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Yes, SecurityTrimming is a great feature when you use both a SiteMap and say, a TreeView. I had forgotten all about that... ;)

However, with a custom XML file, you would still need to manually code some stuff to make this work. Personally, I find the SiteMap sometimes a little too constraining, so I often go for the manual solution.

In your case it should be more than enough.

Thanks for the update.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
While typing this post, I was listening to: Song to say goodbye by Placebo (Track 13 from the album: Meds) What's This?
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help me to solve these two problem khatu_jec C# 4 January 3rd, 2008 08:41 AM
Need help to solve problem PankajMishra XSLT 7 April 19th, 2007 08:27 AM
Will you please help me to solve this problem....? vis_kapadia Excel VBA 2 November 22nd, 2006 04:57 AM
HELP! Can anyone solve this problem? JoeR VB How-To 3 September 5th, 2006 09:56 AM
Solve my problem first? wakil C# 3 February 9th, 2006 08:21 AM



All times are GMT -4. The time now is 01:05 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.