How to: Display Different Information to Anonymous and Logged In Users

You can display different information to anonymous and logged-in (authenticated) users by including a LoginView control on the page. The LoginView control includes two templates: one for displaying information to anonymous users, and the other for displaying information to logged-in users. (You can also include templates based on role names using the RoleGroups property.)The control automatically determines whether a user is authenticated and renders the appropriate template.

To display different information to anonymous and logged-in users

  1. In Design view on an ASP.NET page, from the Login folder in the Toolbox, drag a LoginView control onto the page.

  2. If the LoginView Tasks panel is not showing, right-click the control and click Show Smart Tag to display the LoginView Tasks panel.

    By default, the control displays the AnonymousTemplate.

  3. Add static text and controls that should be visible to users who have not logged in, such as a Login control that enables users to log in, recover a password, or create a new user ID.

  4. In the LoginView Tasks menu, select LoggedInTemplate from the list of Views.

  5. Add static text and controls that should be visible to authenticated users, such as a LoginName control with the FormatString property set to a welcome message.

Example

<asp:LoginView ID="LoginView1" Runat="server">
  <LoggedInTemplate>
   <asp:LoginName ID="LoginName1" Runat="server" 
                  FormatString ="Welcome, {0}"/>
   <br />
    <asp:HyperLink ID="HyperLink1" Runat="server" 
      NavigateUrl="~/MemberPages/ChangePassword.aspx">
      Change Password
    </asp:HyperLink>
  </LoggedInTemplate>
  <AnonymousTemplate>
    <asp:Login id="Login1" runat="server" 
        CreateUserText="Create a new user..."
        CreateUserUrl="~/Register.aspx" 
        PasswordRecoveryUrl="~/Recovery.aspx" 
        UserNameLabelText="E-mail address:" />
  </AnonymousTemplate>
</asp:LoginView>

The code example shows a LoginView control in Source view after you have defined an AnonymousTemplate and a LoggedInTemplate.

See Also

Tasks

Walkthrough: Creating a Web Site with Membership and User Login

Reference

ASP.NET Login Controls Overview