How to display/hidden menu items automatically due to logined user in ASP.NET 2.0

How to display/hidden menu items automatically due to logined user in ASP.NET 2.0

.NET 2.0 has the feature of displaying/hidden menu items automatically due to logined user in ASP.NET 2.0, but you have to know how to set up.

Normally, when you create a new .NET project, you can not get this feature, you will find specific menu item can not hidden or display due to the different users.
Just set up by the following steps:

1: Add a XML SiteMap Provider and something else related section in web.config file:

<system.web>
<authentication mode=”Forms”>

<forms loginUrl=”Default.aspx”  name=”.ASPNETAUTH”  path=”/” timeout=”20″ protection=”Validation” />
</authentication>

<authorization>
<allow users=”*”/>
</authorization>

<customErrors mode=”RemoteOnly” defaultRedirect=”CustomError.aspx”/>
<pages theme=”Standard”/>
<membership>
<providers>
<remove name=”AspNetSqlMembershipProvider”/>
<add name=”AspNetSqlMembershipProvider”


</membership>

<profile>
<providers>
<remove name=”AspNetSqlProfileProvider”/>
<add name=”AspNetSqlProfileProvider”


</providers>
</profile>

<roleManager enabled=”true”>
<providers>
<remove name=”AspNetSqlRoleProvider”/>
<add name=”AspNetSqlRoleProvider”


</providers>
</roleManager>

<siteMap defaultProvider=”XmlSiteMapProvider” enabled=”true”>
<providers>
<add name=”XmlSiteMapProvider” description=”SiteMap provider which reads in .sitemap XML files.” type=”System.Web.XmlSiteMapProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” siteMapFile=”web.sitemap” securityTrimmingEnabled=”true”/>
</providers>
</siteMap>

<!– compilation debug=”true”/ –>

</system.web>

2: Add menu items in Web.sitemap file:

<?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=”Home Page” >
<siteMapNode url=”#” title=”Home” description=”Home Page” />
<siteMapNode url=”~/Services.aspx” title=”Services” description=”Our Services”/>
<siteMapNode url=”~/About.aspx” title=”About Us” description=”About Us”/>
<siteMapNode url=”~/Contact.aspx” title=”Contact” description=”Contact Information”/>
<siteMapNode url=”~/Admin/ContactInfoAdm.aspx” title=”Administration” description=”Administration” />
</siteMapNode>
</siteMap>

Note: in the Web.sitemap, for the “Home” menu, there should be add 2 times in Visual Web Developer 2005 Express like the following :

<siteMapNode url=”~/Default.aspx” title=”Home” description=”Home Page” >
<siteMapNode url=”#” title=”Home” description=”Home Page” />
…..

I tried  in Visual Studio 2005 too, It doesn’t need 2 times, just 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=”Home” description=”Home Page” >
<siteMapNode url=”~/Services.aspx” title=”Services” description=”Our Services”/>
<siteMapNode url=”~/About.aspx” title=”About Us” description=”About Us”/>
<siteMapNode url=”~/Contact.aspx” title=”Contact” description=”Contact Information”/>
<siteMapNode url=”~/Admin/ContactInfoAdm.aspx” title=”Administration” description=”Administration” />
</siteMapNode>
</siteMap>