Better conditional content for anonymous users with LoginView

SharePoint 2010 ships with the SPSecurityTrimmedControl that allows you to conditionally display content to users based on their permissions. On top of that it gives you the ability to display content to anonymous/authenticated users only which unfortunately doesn’t work. And although you might want start off and develop something of your own, it turns out that for all this time there was a solution for this just around the corner.

Conditional content for anonymous users: what for?

There are many scenario’s for which you need to display content to anonymous users only: login links, web analytics tracking codes and SharePoint 2010 adds even one more to this list: hiding the Ribbon bar. There is nothing worse than something on an Internet-facing website that doesn’t belong there like there Ribbon bar.

So while you could develop something that would do the job yourself and sentence yourself for lifetime support of it, it turns out that ASP.NET already has something specially for that purpose: the LoginView control.

Conditionally displaying content to anonymous users using the ASP.NET LoginView control

Conditionally displaying content to anonymous users using the LoginView control is very simple. All you need to do is to add it to your Master Page/Page Layout and wrap the conditional content in the Anonymous Template.

<asp:LoginView runat="server">
        <!-- conditional content here -->

So for example if you wanted to hide the Ribbon bar from anonymous users you would include the following CSS snippet:

<asp:LoginView runat="server">
        <style type="text/css">
            #s4-ribbonrow { display: none; }

Because the CSS would get applied for anonymous users only, content editors would still be able to use the Ribbon: and all that without too much hacking into the platform and not a single line of custom code!

Next to conditionally displaying content to anonymous users, the ASP.NET LoginView control allows you to conditionally display content to authenticated users. This makes it a great solution for conditionally displaying content to users based on their authentication state.

A word about performance

There is one more thing about the ASP.NET LoginView control that makes it really great. In my last article about the SPSecurityTrimmedControl I discussed why it sucked: the way the SPSecurityTrimmedControl does the conditional display is that it first executes all child controls and then at the end checks if they should be rendered or not. A better alternative to that is using templates which prevent instantiating controls that won’t be rendered anyway. And this is exactly what the ASP.NET LoginView control does. Because it uses templates, it first checks if the content should be displayed and if not, nothing happens. Using templates is a great approach from the performance point of view and there is no reason you shouldn’t be using it.


SharePoint 2010 is a great platform that ships with many controls that simplify the process of developing custom solutions. Although in some scenario’s the standard controls provided with SharePoint 2010 are not sufficient it’s worth looking at the controls that are part of the ASP.NET framework instead of developing something yourself. The ASP.NET LoginView is an example of such control which is a great alternative to the SPSecurityTrimmedControl for displaying content to anonymous users.

Technorati Tags: SharePoint 2010,ASP.NET