Part IV: Adding Annymous to a Master Page

Create Anonymous Site
One of the next things I wanted to do is play with having an anonymous site because I know there is a little extra work I need to do master page to handle this correctly. To quickly do this on my dev machine:
  • I extended my web application to have a new internet zone; making sure to allow anonymous access.
  • Then I went to the site via the new url for the zone I just created. I was prompted for a password even though I had anonymous set up. So I needed to do a few other things.
  • I then went to Site Actions >> Site Permissions >> in the ribbon I selected the Anonymous Access button >> and selected Entire Site for anonymous access.
  • I then closed the browser. Then I went Start >> Run As >> typed in the url for the new zone I created.
  • NOTE – make sure you check in your Master Page! Otherwise you will see an unbranded website. I did this by mistake because I was logged in as an Administrator on my development machine but going to the SharePoint site as an anonymous user. Anonymous users cannot see anything that has not been checked in and published.
Below is the result. It still looks the same other than the ribbon at the top has a lot less stuff. At this point the ribbon really does not do much good for an anonymous user and the first request is can this be hidden for anonymous users?


There have been several approaches written about this on the internet. The most simple and clean solution is to add some CSS to the master page to handle this. So I will add the asp:LoginView control into the master page. Any code that I added within the <AnonymousTemplate> will only appear when the user is logged in anonymously. So what I did was add the link to the login page within the <AnonymousTemplate> tags. This way a user will see a link to login when they are anonymous and then not see the link anymore after that.
Below I have added it into the master page with my top navigation. You can put it where ever you want to.

<!-- header -->
<div id="header" class="s4-notdlg">
    <div id="menu">
        <div id="menu_list">          
     
            <asp:LoginView id="LoginView" runat="server">
                <AnonymousTemplate>
                    <a href="/_layouts/authenticate.aspx" title="User Login">Sign In</a>
                    <style type="text/css">
                        #s4-ribbonrow {display: none;}
                    </style>
                </AnonymousTemplate>     
            </asp:LoginView>

     
            <!-- top navigation area -->
            <div class="s4-notdlg">
                <!-- top navigation publishing data source -->
                <PublishingNavigation:PortalSiteMapDataSource
                    ID="topSiteMap"
                    runat="server"
                    EnableViewState="false"
                    SiteMapProvider="GlobalNavigation"
                    StartFromCurrentNode="true"
                    StartingNodeOffset="0"
                    ShowStartingNode="false"
                    TrimNonCurrentTypes="Heading"/>
     
                <!-- top navigation menu (set to use the new Simple Rendering) -->
                <SharePoint:AspMenu
                  ID="TopNavigationMenuV4"
                  EncodeTitle="false"
                  Runat="server"
                  EnableViewState="false"
                  DataSourceID="topSiteMap"
                  AccessKey="<%$Resources:wss,navigation_accesskey%>"
                  UseSimpleRendering="true"
                  UseSeparateCss="false"
                  Orientation="Horizontal"
                  StaticDisplayLevels="1"
                  MaximumDynamicDisplayLevels="1"
                  SkipLinkText=""
                  CssClass="s4-tn">
                </SharePoint:AspMenu> 
            </div>
        </div>
    </div>
</div>

Then when I went back to the site, this is what I now see. I now see a link called Sign In. 


 Then after I press the Sign In link and log in, the ribbon is displayed again and that Sign In link is hidden from view. Pretty cool.