Using the LoginView Control


Using the LoginView Control

The LoginView control enables you to display different content to different users depending on their authentication status. For example, the page in Listing 20.38 displays different content for authenticated users and anonymous users (see Figure 20.16).

Listing 20.38. ShowLoginView.aspx

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Show LoginView</title> </head> <body>     <form  runat="server">     <div>     <asp:LoginStatus                  Runat="server" />     <hr />     <asp:LoginView                  Runat="server">         <AnonymousTemplate>         This content is displayed to anonymous users.         </AnonymousTemplate>         <LoggedInTemplate>         This content is displayed to authenticated users.         </LoggedInTemplate>     </asp:LoginView>     </div>     </form> </body> </html> 

Figure 20.16. Displaying content to authenticated users with the LoginView control.


The LoginView control in Listing 20.38 contains two templates: an AnonymousTemplate and a LoggedInTemplate. Only one of the two templates is displayed at a time.

The page also includes a LoginStatus control. You can use this control to log in and log out quickly.

Note

You can use the LoginView control with Windows authentication as well as Forms authentication.


Using Roles with the LoginView Control

You also can use the LoginView control to display different content to users who belong to different roles. The page in Listing 20.39 contains a LoginView that contains two RoleGroup controls. The first RoleGroup contains content that is displayed to members of the Administrator role. The second RoleGroup contains content that is displayed to members of the Manager and Worker roles.

Listing 20.39. LoginViewRoles.aspx

[View full width]

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">     Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)         Dim status As MembershipCreateStatus         ' Create Bill         Membership.CreateUser("Bill", "secret_", "bill@somewhere.com", "dog", "rover",  True, status)         ' Create Ted         Membership.CreateUser("Ted", "secret_", "ted@somewhere.com", "dog", "rover", True,  status)         ' Create Fred         Membership.CreateUser("Fred", "secret_", "fred@somewhere.com", "dog", "rover",  True, status)         ' Create Administrator Role         If Not Roles.RoleExists("Administrator") Then             Roles.CreateRole("Administrator")             Roles.AddUserToRole("Bill", "Administrator")         End If         ' Create Manager Role         If Not Roles.RoleExists("Manager") Then             Roles.CreateRole("Manager")             Roles.AddUserToRole("Bill", "Manager")             Roles.AddUserToRole("Ted", "Manager")         End If         ' Create Worker Role         If Not Roles.RoleExists("Worker") Then             Roles.CreateRole("Worker")             Roles.AddUserToRole("Fred", "Worker")         End If     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>LoginView Roles</title> </head> <body>     <form  runat="server">     <div>     <asp:LoginStatus                  Runat="server" />     <hr />     <asp:LoginView                  Runat="server">         <RoleGroups>         <asp:RoleGroup Roles="Administrator">         <ContentTemplate>         This content is displayed to Administrators.         </ContentTemplate>         </asp:RoleGroup>         <asp:RoleGroup Roles="Manager,Worker">         <ContentTemplate>         This content is displayed to Managers         and Workers.         </ContentTemplate>         </asp:RoleGroup>         </RoleGroups>     </asp:LoginView>     </div>     </form> </body> </html> 

The Page_Load() handler in Listing 20.39 creates three users named Bill, Ted, and Fred. Bill is added to both the Administrator and Manager roles, Ted is added to the Manager role, and Fred is added to the Worker role.

The content of only one RoleGroup is displayed by the LoginView control at a time. If a user matches more than one RoleGroup, then the content of the first RoleGroup matched is displayed and the other RoleGroups are ignored.

Before you can use the page in Listing 20.39, you must enable roles in the web configuration file. The file in Listing 20.40 contains the necessary roleManager element.

Listing 20.40. Web.Config

<?xml version="1.0" encoding="utf-8"?> <configuration>   <system.web>     <authentication mode="Forms" />     <roleManager enabled="true" />   </system.web> </configuration> 




ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net