SharePoint Authenticated User Panel

When using SharePoint 2007 (MOSS) or SharePoint 2010 as a web content management system, it’s common to have pages show different content to logged in users and anonymous users.

Sometimes you can achieve what you want via audiences or security trimming, but often I find it’s easier to use an overloaded panel that you can include in your page layouts or master pages.  You can then set public properties to determine who the content renders for (anonymous users, specific roles, etc).

 

<p>    <span class="kwrd">public</span> <span class="kwrd">class</span> AuthenticatedPanel : Panel
    {
        <span class="kwrd">public</span> <span class="kwrd">bool</span> ShowForAnonymous { get; set; }
        <span class="kwrd">public</span> <span class="kwrd">bool</span> ShowForAuthenticated { get; set; }
        <span class="kwrd">public</span> <span class="kwrd">string</span> ShowForRole { get; set; }

        <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Render(HtmlTextWriter writer)
        {
            <span class="kwrd">if</span> (ShowForAnonymous)
            {
                <span class="kwrd">base</span>.Render(writer);
                <span class="kwrd">return</span>;
            }

            <span class="kwrd">if</span> (ShowForAuthenticated &amp;&amp; </p><p>                HttpContext.Current.User.Identity.IsAuthenticated)
            {
                <span class="kwrd">base</span>.Render(writer);
                <span class="kwrd">return</span>;
            }

            <span class="kwrd">if</span> (!String.IsNullOrEmpty(ShowForRole) &amp;&amp; </p><blockquote><p>                HttpContext.Current.User.IsInRole(ShowForRole))
            {
                <span class="kwrd">base</span>.Render(writer);
                <span class="kwrd">return</span>;
            }
            
        }
        
    }</p></blockquote>
Share This Post!Tweet about this on TwitterShare on Google+Share on FacebookShare on RedditEmail this to someoneShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>