Better conditional content for anonymous users with LoginView
ASP.NET, Best Practices, Development, Productivity, SharePoint 2010, Tips & Tricks, WCM
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">
<AnonymousTemplate>
<!-- conditional content here -->
</AnonymousTemplate>
</asp:LoginView>
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">
<AnonymousTemplate>
<style type="text/css">
#s4-ribbonrow { display: none; }
</style>
</AnonymousTemplate>
</asp:LoginView>
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.
Summary
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.


















October 15th, 2010 at 9:00 pm
Hi Waldek Thanks for sharing Information.
I am working on master page which is developed by third party consultant and the have used securityTrimmedControl and then i read this post and thought of giving try to use this control to hide ribbon for anonymous users
so here what i did
—code for ribbon goes here.
well its worked fine but then today when try to modify Page i have to click twice edit page in site actions menu in order to get page layout edit mode.
but with security control it works fine.
any inside in this
October 16th, 2010 at 11:24 am
@Ronak: Unfortunately the Ribbon is one of the controls that doesn't really support conditional rendering/processing. So far the only way to get rid of it is to use conditional CSS to hide the remains of it from anonymous users.
January 27th, 2011 at 2:52 pm
Waldek, I may be opening a can of worms, but do you know at what point in the page lifecycle does the ribben get rendered?
As Ronak says, using asp:LoginView breaks page editing to various degrees, and I'd like to try to hide or display the ribbon by overriding a method in masterpage's codebehind.
Thanks,
Chris
January 27th, 2011 at 3:16 pm
@Chris: From I've noticed is that it has something to do with the asynchronous processing of the Ribbon. While it used to break in the betas but if I recall correctly you should be able to solve it using the SPSecurityTrimmedControl.