Master Page:
<ul>
<li class="first"><asp:HyperLink runat="server" ID="lnkHome"
NavigateUrl="~/Forms/Default.aspx">Home</asp:HyperLink></li>
<asp:Repeater runat="server" ID="Repeater1" DataSourceID="SiteMapDataSource1"
EnableViewState="False">
<ItemTemplate>
<li>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("Url") %>'><%# Eval("Title") %>
</asp:HyperLink>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
Web.SiteMap
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode id="SiteHome" url="~/Forms/Default.aspx" title="Home"
description="Default">
<siteMapNode id="SiteAbout" url="~/Forms/Aboutus.aspx" title="About us"
description="About Us"/>
<siteMapNode id="SiteNew" url="~/Forms/New.aspx" title="New"
description="My Sample Page"/>
</siteMapNode>
Design:
HOME | About us | My Sample Page
I am new on ASP.Net Webforms and i am creating my first Project.
I am using Repeater to navigate page urls.
On my Master/Page.cs Code Behind I need to Hide
[My Sample Page] based on my Login Account.
something like:
if(UserGroup="Admin")
{
//Show My Sample Page
HOME | About us | My Sample Page
}
else if(UserGroup="User1")
{
//Hide About Us
HOME | About us
}
else
{
// Show Home Only
HOME
}
How to do it in form Load?
Thanks in Regards..
The correct way to do this is define a site map provider in your Web.Config and make sure you set the attribute securityTrimmingEnabled="true".
<siteMap defaultProvider="MySiteMap">
<providers>
<clear/>
<add
name="MySiteMap"
type="System.Web.XmlSiteMapProvider"
securityTrimmingEnabled="true"
siteMapFile="~/Web.SiteMap" />
</providers>
</siteMap>
In your Forms folder create a Web.Config file and include the following
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
<location path="New.aspx">
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</location>
</configuration>
Now ASP.Net will take care of showing which parts of your site map to which users.
Eg: In this case it will hide your My Sample Page page from un-authenticated users.
Try something like:
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Url") %>'
Visible='Eval("Title") = "My Sample Page" ? this.IsAdmin.ToString() : "True"' >
<%# Eval("Title") %>
</asp:HyperLink>
Related
I'm working with a project that has as ASP menu (), which I can't get to close. All I want it to do is to collapse when the mouse is not hovering it. Is there a setting or something to fix this? Maybe a simple problem but I've searched everywhere with no luck.
I can expand the menu just fine, and when I move to a different node it switches to that just. The problem is when I move the mouse outside the entire menu, it stays open. Shouldn't there be some simple setting for this? Do I really have to jump through several hoops of javascript and events to fix this?
By default, menu should collapse as soon as cursor is out. Strip out all css and test it with simple code.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="MenuTest.aspx.cs" Inherits="WebApplication2012.MenuTest" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Menu runat="server" DataSourceID="MenuDataSource"></asp:Menu>
<asp:SiteMapDataSource ID="MenuDataSource" runat="server" ShowStartingNode="false"
SiteMapProvider="XmlSiteMapProvider" />
</form>
</body>
</html>
// MenuTest.sitemap
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="Home" title="Home" description="Home">
<siteMapNode url="Monday" title="One" description="One" >
<siteMapNode url="Monday - One" title="Monday - One" description="Monday - One" />
<siteMapNode url="Monday - Two" title="Monday - Two" description="Monday - Two" />
<siteMapNode url="Monday - Three" title="Monday - Three" description="Monday - Three" />
</siteMapNode>
<siteMapNode url="Tuesday" title="Tuesday" description="Tuesday" />
</siteMapNode>
</siteMap>
// web.config
<system.web>
...
<siteMap defaultProvider="XmlSiteMapProvider">
<providers>
<add name="XmlSiteMapProvider" type="System.Web.XmlSiteMapProvider" siteMapFile="~/MenuTest.sitemap" />
</providers>
</siteMap>
</system.web>
I have a .sitemap file that contains all of my nodes that I want to display for my side navigation. I am having trouble displaying all of my nodes when I get to the sub level. Everything just disappears when I visit the sub-page. In otherwords, when I am in the homepage.aspx page, all of my nodes are visible. When I enter the level1a.aspx page, all of the nodes disappear. I have no code behind. Is there a setting to display all nodes? Or would I need to write code to display them all?
Here are files:
web.config
<siteMap>
<providers>
<add name="SiteMap" type="System.Web.XmlSiteMapProvider" siteMapFile="~/my.sitemap" />
</providers>
</siteMap>
control.ascx
<asp:SiteMapDataSource ID="SiteMapDataSource1" SiteMapProvider="SiteMap" runat="server" />
<asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1"></asp:Menu>
my.sitemap
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="/HomePage.aspx" title="Home Page" description="">
<siteMapNode url="/level1.aspx" title="Level 1" description="">
<siteMapNode url="/level1a.aspx" title="Level 1 A" description="" />
<siteMapNode url="/level1b.aspx" title="Level 1 B" description="" />
</siteMapNode>
</siteMapNode>
</siteMap>
I just made the experiment and it works fine...
Double check all your pages contain a reference to your user control
This is what I did
UC
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MyMenuUC.ascx.cs" Inherits="WebApplication1.MyMenuUC" %>
<asp:SiteMapDataSource runat="server" ID="mySiteMapDataSource" ShowStartingNode="false" />
<asp:Menu runat="server" DataSourceID="mySiteMapDataSource"></asp:Menu>
Web.sitemap
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="~/Default.aspx" title="Home" description="">
<siteMapNode url="~/MenuWithUC1.aspx" title="page 1" description="" />
<siteMapNode url="~/MenuWithUC2.aspx" title="page 2" description="" />
</siteMapNode>
</siteMap>
MenuWithUC1.aspx
<%# Register Src="~/MyMenuUC.ascx" TagName="SharedMenu" TagPrefix="menu" %>
....
<menu:SharedMenu runat="server" />
<asp:Button ID="Button1" Text="post me" runat="server" />
MenuWithUC2.aspx
<%# Register Src="~/MyMenuUC.ascx" TagName="SharedMenu" TagPrefix="menu" %>
....
<menu:SharedMenu runat="server" />
<asp:Button Text="post me" runat="server" />
The buttons on each page are just to test that the Menu control keeps its state on each post
try with EnableViewState = true
<asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1" EnableViewState="true"></asp:Menu>
I can't figure this one out.
I have the following SiteMap
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="~/" title="Root" description="Go root">
<siteMapNode url="~/h" title="Home" description="Go home" />
<siteMapNode url="~/h/uo" title="Ultima Online" description="Ultima Online">
<siteMapNode url="~/h/uo/get" roles="RegisteredUser" title="Get account!" description="Get account!" />
</siteMapNode>
</siteMapNode>
</siteMap>
I've an XmlSiteMapProvider with securityTrimmingEnabled="true", which points to this site map file.
The file I want to trim has an authorization rule in it's folder's web.config
<configuration>
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</configuration>
The file can't be accessed via url, if I type http://localhost/h/uo/get I get redirected to login page.
I've set up an <asp:Menu> like this in the Master page file:
<asp:SiteMapDataSource ID="MenuSiteMap" ShowStartingNode="false"
SiteMapProvider="MenuSiteMapProvider" runat="server"
/>
<div>
<asp:Menu ID="NavigationMenu" runat="server" DataSourceID="MenuSiteMap"
CssClass="menu" EnableViewState="false"
IncludeStyleBlock="false" Orientation="Horizontal"
/>
</div>
Yet, when the page is rendered, I see the Get account node that is supposed to be trimmed when I'm not even logged in, no matter what.
What am I doing wrong?
Is there any other way to build a security trimming enabled site map navigation menu?
I'm using ASP.NET 4.0, and URL-rewritting with an HttpModule.
In reading http://forums.asp.net/t/975077.aspx/1 I found out that this is exactly what is happening to me.
If the node doesn't have an URL it behaves fine, but if it does, like all of my nodes do. Security trimming is just ignored.
I resolved my problem by resorting to a more intuitive role based site map implementation, to say:
public class TrimmingXmlSiteMapProvider : XmlSiteMapProvider
{
public override bool IsAccessibleToUser(HttpContext context, SiteMapNode node)
{
if (node.Roles.Cast<string>().Any(r => r == "*"))
return true;
if (node.Roles.Count > 0 && node.Roles.Cast<string>().Count(Roles.IsUserInRole) == 0)
return false;
return node.ParentNode != null && node.ParentNode.IsAccessibleToUser(context);
}
}
Then, the only change I had to make was add an asterisk to the root level's role definition.
How does this work?
First I check if any of the roles definied for this node is an asterisk, if that's the case, then I can see the node.
Second, if the node isn't everyone-level, I check if there are any roles specified, and if the logged in user is part of at least one of them.
Lastly, I check if there is a parent node, and just inherit their rule.
This allows the security trimming to actually be "SECURITY TRIMMING" and not well, however the heck it's supposed to be working by default.
I have sitemap which looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="~/default.aspx" title="Prva stran" roles="*" description="Shema ISEF">
<siteMapNode roles="2" title="Analize" id="Analize" description="" >
<siteMapNode url="~/karneki1.aspx" title="Karneki1" description="" />
<siteMapNode url="~/karneki2.aspx" title="Karneki2" description="" />
</siteMapNode>
</siteMapNode>
</siteMap>
if I set roles in a siteMapNode with title "Analiza" it works fine, the link is not shown in the navigation... but if I set roles on any of "karneki" siteMapNode the links are still visible...
Is it even posible to restrict access to lower links based on user role?
Use the SiteMap 'securityTrimmingEnabled' attribute:
http://msdn.microsoft.com/en-us/library/ms178428.aspx
The asecurityTrimmingEnabled attribute also needs to be added to the nodes in the markup:
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
An overview of how securityTrimmingEnabled is supposed to work:
http://blogs.msdn.com/b/dannychen/archive/2006/03/16/553005.aspx
The solution to this is that you need to set the roles in a Web.config for the pages itself.
See 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
e.g. in the folder for a page called AdminOnly.aspx add a Web.Config with the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="AdminOnly.aspx">
<system.web>
<authorization>
<allow roles="Administrator"/>
<deny users="*" />
</authorization>
</system.web>
</location>
</configuration>
Try to add the roles to the site map like this
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="~/default.aspx" roles="*" title="Prva stran" roles="*" description="Shema ISEF">
<siteMapNode roles="2" title="Analize" id="Analize" description="" >
<siteMapNode roles="*" url="~/karneki1.aspx" title="Karneki1" description="" />
<siteMapNode roles="*" url="~/karneki2.aspx" title="Karneki2" description="" />
</siteMapNode>
</siteMapNode>
</siteMap>
I'm trying to make a menu based off of an asp.net sitemap. How do you nest the sitemap nodes so that they all appear on the same level. Here is what I have:
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode>
<siteMapNode url="~/Default.aspx" title="Home" description="link to Home" />
<siteMapNode url="~/about.aspx" title="About" description="abot" />
</siteMapNode>
</siteMap>
Here is what the code for the Menu control looks like:
<asp:Menu ID="Menu1" runat="server" BackColor="#E3EAEB"
DataSourceID="SiteMapDataSource1"
</asp:Menu>
They both appear as 2nd tier elements underneath an arrow. Sorry for the beginner question but I've never used the menu control before.
You just need to set the StaticDisplayLevels and only have one level in the sitemap file.
<asp:Menu runat="server" DataSourceID="SiteMapDataSource" StaticDisplayLevels="2" >
</asp:Menu>
An example of the web.sitemap:
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode>
<siteMapNode url="Default.aspx" title="Home" description="" />
<siteMapNode url="Page2.aspx" title="Page2" description="" />
</siteMapNode>
</siteMap>