Hiding content from anonymous users

Have you ever put together a master page for a public facing MOSS site and found that you are left with the “sign in” link where the user menu is? Or do you not like the empty tables that get rendered to your sites visotrs that are normally there to house the publishing console? Well fear no more, there is a very, very easy way to hide content in a master page or page layout from anonymous users. This method takes the idea of inheriting from the SPSecurityTrimmed control that I used when I wanted to hide content when a specific publishing field was blank, and applies it to hiding content when a user is anonymous, here is the code:

    public class HideFromAnonymousUser : SPSecurityTrimmedControl
    {
        protected override void Render(HtmlTextWriter Output)
        {
            if (HttpContext.Current.Request.IsAuthenticated)
            {
                base.Render(Output);
            }
        }
    }

This is really basic, basically we just check to see if the call to the page is authenticated, and if it is we render the contents. This means we can hide any part of a master page we want by using the control like this:

<div id="userBar">
<table cellpadding="0" cellspacing="0" align="right" border="0">
<tr>
<td>
<wssuc:Welcome id="ExplitLogout" runat="server" />
</td>
<td>
<PublishingSiteAction:SiteActionMenu runat="server" />
</td>
</tr>
</table>
</div>

In this example I have a table with the user menu and site actions menu in it, so stuff I obviously don’t want my anonyous users knowing is there. By wrapping it up in the hide from anonymous user tag, any anonymous user won’t only not see the controls, but they will also not see the table that I’m using to lay them out the way I want as well!

  1. No trackbacks yet.