asp.netsitemapaspmenu

Change the link on a sitemap based on if a user is logged in?


I have a sitemap that has a link for when a user is not logged in, but when they do login, the link should change, for example, nonmember.aspx should change to member.aspx. This sitemap is tied to an asp:menu. Does anyone know how to do this?


Solution

  • A simple solution is to have two nodes in your sitemap.

    1. One node shows up for Anonymous users.
    2. One node shows up for Authenticated users with the security access

    I believe you can set this up quite simply.

    The end result is the same as changing the link but it's easier to maintain.

    To add to this:

    <?xml version="1.0" encoding="utf-8" ?>
    <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
        <siteMapNode title="Home" url="~/" roles="*">
            <siteMapNode url="~/Member.aspx" title="Home" roles="SpecialPeople" />
            <siteMapNode url="~/Nonmember.aspx" title="Site Map" roles="HideForUsers" />
        </siteMapNode>
    </siteMap>
    

    So, you set up a rule that denies access to the "HideForMembers" role to authenticated users. It's something like that. ASP.NET will take the first rule it finds a match, so you should be able to accomplish it this way.

    Otherwise, you could do a Menu_OnDataBound and look for the node:

    Protected Sub menMainDataBound(ByVal sender As Object, ByVal e As System.EventArgs)
    
        Try
    
            Dim myPage As New Page
            Dim myPrincipal As IPrincipal
            Dim colNodes As New Collection
    
            myPrincipal = myPage.User
    
            If myPrincipal.Identity.IsAuthenticated = True Then
    
                    Dim menNode As MenuItem
    
                    For Each menNode In menMain.Items
    
                        Select Case menNode.Value.ToString
                            Case "Products"
                                colNodes.Add(menNode)
                            Case "Contact Us"
                                colNodes.Add(menNode)
                            Case "About Us"
                                colNodes.Add(menNode)
                            Case "Links"
                                colNodes.Add(menNode)
                        End Select
    
                    Next
    
                    For Each menNode In colNodes
                        menMain.Items.Remove(menNode)
                    Next
    
             End If
    
        Catch ex As Exception
    
        End Try
    
    End Sub
    

    source