Guys i need to validate a menu item on my ASP.NET application. The menu item has 4 links (Home, News, About us and Contact us) one of them "News" must not redirect to page if the user has not log on, the other three can redirect even if the user has'nt loggin or log in.
Please help with validating this process using C#

Dani AI

Generated

Two practical options were already suggested. ’s MenuItem handler is a quick UI-level fix, but it’s brittle (reading a control’s display text or UI state is fragile) and it doesn’t stop a user from entering the News URL directly. The resource itself must be protected with URL authorization — hiding a link is only UX-level protection. (learn.microsoft.com)

A more robust approach is to enable ASP.NET security trimming and drive the Menu from a Web.sitemap (SiteMapDataSource → Menu). With security trimming turned on the framework will automatically hide nodes the current user cannot access (based on URL authorization and role checks). Steps:

  1. Add a Web.sitemap describing Home/News/About/Contact.
  2. Enable security trimming on the siteMap provider in Web.config.
  3. Bind the Menu to a SiteMapDataSource on the page or master page. Example snippets:
<system.web>
  <siteMap defaultProvider="XmlSiteMapProvider" enabled="true">
    <providers>
      <add name="XmlSiteMapProvider"
           type="System.Web.XmlSiteMapProvider"
           siteMapFile="Web.sitemap"
           securityTrimmingEnabled="true" />
    </providers>
  </siteMap>
</system.web>
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
  <siteMapNode title="Root" url="~/Default.aspx">
    <siteMapNode title="Home" url="~/Home.aspx" />
    <siteMapNode title="News" url="~/News.aspx" />
    <siteMapNode title="About Us" url="~/AboutUs.aspx" />
    <siteMapNode title="Contact Us" url="~/ContactUs.aspx" />
  </siteMapNode>
</siteMap>

Bind the menu to the SiteMapDataSource (for example, SiteMapDataSource + Menu.DataSourceID), and the framework will hide News for users who can’t access it. See the walkthrough for enabling security trimming. (learn.microsoft.com)

Protect the News page itself with URL authorization so anonymous users are denied; security trimming will then hide the node and direct requests will be blocked (Forms auth will redirect to login). Example:

<location path="News.aspx">
  <system.web>
    <authorization>
      <deny users="?" />
    </authorization>
  </system.web>
</location>

Notes: security trimming checks URL/role rules and can cost extra work for very large sitemaps — setting explicit roles on nodes can improve performance. If you keep the Menu1_MenuItemClick route, check Page.User.Identity.IsAuthenticated (or role checks) rather than reading a UI control’s text. Always enforce page-level authorization; hiding links is not a substitute for real access control. (learn.microsoft.com)

Recommended Answers

All 4 Replies

Add below code to aspx page

<asp:Menu ID="Menu1" runat="server"  Width="164px" OnMenuItemClick="Menu1_MenuItemClick" >
           <Items>
               <asp:MenuItem Text="Home" Value="Home" NavigateUrl="Home.aspx"></asp:MenuItem>
               <asp:MenuItem Text="News" Value="News" NavigateUrl="News.aspx"></asp:MenuItem>
               <asp:MenuItem Text="About Us " Value="About Us " NavigateUrl="AboutUs.aspx"></asp:MenuItem>
               <asp:MenuItem Text="Contact us" Value="Contact us" NavigateUrl="ContactUs.aspx"></asp:MenuItem>
           </Items>
       </asp:Menu>
        <asp:LoginStatus ID="LoginStatus1" runat="server" Style="z-index: 100; left: 39px;
            position: absolute; top: 185px" Width="116px" />

Add the below code to Page

protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
        if (Menu1.SelectedItem.Text == "News")
        {
            if (LoginStatus1.LoginText == "LogOut")
            {
                Menu1.SelectedItem.Enabled = false;
            }
        }
        else
        {
            Menu1.SelectedItem.Enabled = true;
        }
    }

Try this ..Chithra

The better way would be to enable security trimming in the web.sitemap or whichever xml file that you have used as a source to your menu.

The better way would be to enable security trimming in the web.sitemap or whichever xml file that you have used as a source to your menu.

This security trimming is a new thing to me, tell me more about how do i go about this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.