Web.sitemap display links in function of role - asp.net

I have one Web page , that can be only accessed if user has one of two allowed roles "Politician" or "Worker". Dependending from which user role is accessed the title of the web in the navigation sitemap has to be different.
In our web.config this page is configured to be accessed with the two roles "Politician" or "Worker", and in web.sitemap I have the next code.
<siteMapNode url="DoTask.aspx?Validator=Worker" title="TitleForRole" roles="Worker" />
<siteMapNode url="DoTask.aspx?Validator=Politician" title="TitleFolRoleB" roles="Politician" />
The problem is with that code is despite of user has only one of the roles in the sitemap appear the two links.
Is there a way to make only visible the link user has the role in sitemap?
One solution is create a page for one of the roles that redirect to DoTask.aspx and change sitemap
<siteMapNode url="DoTask.aspx?Validator=Worker" title="TitleForRole" roles="Worker" />
<siteMapNode url="RedirectToDoTask.aspx" title="TitleFolRoleB" roles="Politician" />
But I think this is not elegant solution, I would like to avoid to create another page.
Thanks for your help.

You can do it following the steps in http://weblogs.asp.net/jgalloway/archive/2008/01/26/asp-net-menu-and-sitemap-security-trimming-plus-a-trick-for-when-your-menu-and-security-don-t-match-up.aspx

Related

NavigateURL in MVC has extra domain

So I have an asp.net MenuItem user control created ( similar to what this post has ) within inside a MVC view. The user control works as expected, except the NavigateUrl.
This is the menuitem inside the usercontrol.
<asp:MenuItem Text="View Account" NavigateUrl="~/pages/account.aspx" />
The link looks fine if the user control is being rendered from a webform, it shows
http://localhost/SampleTest/pages/account.aspx
However, put the usercontrol inside the mvc view will have it rendered as
http://localhost/SampleTest/SampleTest/pages/account.aspx
There's an extra domain name "SampleTest" in the url.
What should I do to get rid of the extra domain name?
you need to update your URL navigation to URL.content.
<asp:MenuItem Text="View Account" NavigateUrl="#URL.content("~/pages/account.aspx")" />
code is not tested, but you can do like these.

How to add more root level items to asp.net menu control?

Added a menu control and selected new datasource: SiteMapDataSource1
Here is my Web.sitemap so far:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="~/Home.aspx" title="Home" description="Home Page">
<siteMapNode url="~/PostItem.aspx" title="Post Item" description="Post a new item" />
</siteMapNode>
</siteMap>
How can I add more root level items? I can create more siteMapNode's within the root level one that's already there, but if I add any outside of that it gives me an error. It makes sense to me why that would cause an error, but I'm thinking there's obviously a way to get the menu to display multiple root level menu items... but how?
--Edit--
If you want to build the Menu control with a xml data set, then you can have multiple nodes at the root level. This can be done via the designer and specifying MenuItems in the Items set of the Menu control, you can also add MenuItems to the Items list in the code behind, or bind it to an xml data set.
The MSDN has more information regarding this control. Code Project has a tutorial on using the xml data to build the Menu control.
--Original--
Sorry, you cannot add more than one root level siteMapNode. Typically you see that being the main entry point of the site (like you have it configured). Additional navigation is added from there.
Take a look at the MSDN for more information.

Some user controls not loading when the 'Back' button is pressed

I'm working with ASP.NET and VB.NET.
I have a page which contains 5 user controls.
The page itself does nothing, it just presents the user controls to the user.
The user controls work fine when I come on the page, or when I refresh, but when I go to another page, and press 'Back' only two of my user controls shows up, and they don't work like they're supposed to!
Short description of the controls:
2 controls which look if the user has to create something and shows an option to do so. (goes to the DB)
2 controls show an overview if needed (goes to the DB)
1 control looks if the user is an admin (does not go to the DB, but looks in the securityContext)
So what happens is:
the first 3 controls don't show up
1 control shows his default state (although depending on the user it shouldn't)
the admin control always shows the admin link, even if the user isn't an admin
I've tried looking at caching (disabling, enabling), I've tried cheating the viewstate, I've tried using a placeholder, page_init, ..
Nothing seems to work though, and I'd like this to actually work, 'cause I'm pretty sure users will do this a lot. When I refresh everything works like it should again, because the server gets contacted again..
What should I do? :/
Some code:
<pm:MenuA ID="MenuA" runat="server" />
<pmm:MultiStepMenuA ID="MultiStepMenuA" runat="server" />
<rm:MenuB ID="MenuB" runat="server" />
<mm:MenuC ID="MenuC" runat="server" />
<br />
<am:AdminMenu ID="AdminMenu" runat="server" />
IMPORTANT UPDATE: I don't have this problem with Firefox, only IE9 :/
Blame internet explorer..
Apparently it kept on using an older version of my website "to improve speeds". I removed my history, and I unchecked the "Preserve favorite website data".
After that it worked. The older page was gone, and it was replaced by a newer version.
Thanks anyway, I hope this can help somebody else!
The easiest solution for this problem is to add the following directive at the top of the page to prevent IE or IIS from caching this page:
<%# OutputCache location="None" %>
You can apply this setting globally to all pages by adding the following to the system.web portion of your web.config:
<caching>
<outputCache enableOutputCache="false" enableFragmentCache="false" omitVaryStar="true" />
</caching>

asp.net more than one sitemap

I am having an issue with the sitemap control, I have added the hierarchical levels within the web.sitemap file and added the sitemap control to the various masterpages. I have however two homepages which can be viewed depending on the user of the system.
Is there any way to define two sitemap structures within this web.sitemap file or can I just create another sitemap file?
I am getting
Home > Home > View Details
Instead of
Home > View Details (for my first user)
Thanks
Yes you can define two sitemap structures by creating a seperate sitemap file. Once your sitemaps have been created, all you need todo is to state them within your Web.Config file like so:
<siteMap>
<providers>
<add name="Homepage1" type="System.Web.XmlSiteMapProvider" siteMapFile="~/Homepage1.sitemap" />
<add name="Homepage2" type="System.Web.XmlSiteMapProvider" siteMapFile="~/Homepage2.sitemap" />
</providers>
</siteMap>
Then you can choose which sitemap you want to choose by using the SiteMapDataSource control:
<asp:SiteMapDataSource ID="SitemapDS" runat="server" ShowStartingNode="false" SiteMapProvider="Homepage1" />
Hope this helps.

SiteMapPath doesnt' show Web.sitemap siteMapNode with querystring in url

I have the following web.sitemap portion:
<siteMapNode title="MENU" url="">
<siteMapNode url="~/page1.aspx?id=0" title="MENU 1" />
<siteMapNode url="~/page2.aspx" title="MENU 2" />
</siteMapNode>
When I am in http://localhost/page1.aspx I cannot see the map path because in my sitemap I have quesrystring ?id=0.
Can I fix it?
Thank you soo much!
Nicola
Now that there is a possibility to be in page1.aspx without any querystring so add it to your sitemap otherwise if there is not a way of use without querystrings. so what are you doing in page1.aspx alone? just don't let users to navigate to page1.aspx alone.
you want to display specific value in navigation bar so you cannot do that with that way you have more ways
1-use site map provider
2-make data source which retrieve data from stored procedure or method
I will send you example soon.
best regards

Resources