Displaying specific content only to anonymous users

Similar to the previous recipe, it is common to display certain content on a page only to users who have not logged in. In this recipe, we will add some content to our customized master page that only anonymous users can see.

To see the results of this recipe, you will need to be able to access the SharePoint site both anonymously and logged in.

How to do it...

To display specific content only to anonymous users, we will use the following steps:

  1. Open SharePoint Designer.
  2. Select Open Site. Enter the full URL to the SharePoint site and select Open.
  3. From the Navigation pane, select Master Pages.
  4. In the list of files in the Master Pages library, make a copy of seattle.master (for our example, we have renamed it Seattle_DisplayAnonymousContent.master).
  5. Check out the new Seattle_DisplayAnonymousContent.master master page.
  6. Open the Seattle_DisplayAnonymousContent.master master page.
  7. Locate the element with the ID_SuiteBarBrandingDelegate ID.
  8. After the delegate control, add an <asp:LoginView> control with content in the AnonymousTemplate.
    <asp:LoginView ID="customLoginView" runat="server">
    <AnonymousTemplate>
    <span style="color: #FFFFFF; float: right;">Only Anonymous Users Can See This</span>
    </AnonymousTemplate>
    </asp:LoginView>
  9. Save the master page.
  10. Check in and publish the master page using the Check In and Publish options.
  11. Set the master page as the Site Master Page.
  12. Navigate to the site in your preferred web browser as an anonymous user and as an authenticated user to observe the results.

How it works...

The ASP.NET LoginView control is a simple server control that can have a template for logged in users and a template for anonymous users. The AnonymousTemplate content only renders when a user is not logged in. In this recipe, we added text to the AnonymousTemplate that only users who are not logged in should see.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.37.136