ASP.Net 2.0 LoginView Control
LoginView provides the functionality to create different views for the users of different Roles. LoginView control consists of 3 different templates:
AnonymousTemplate of LoginView control can be used to display the default view of the LoginView that can be viewed by unauthenticated, guest or anonymous user. You can place the link to login page or login control inside the AnonymousTemplate. LoginView Control displays the content and controls of AnonymousTemplate if there is no user logged in.
LoginView Control detects the login status of the user, if membership provider gets the status as authenticated user has logged in then it displays the view provided by the LoggedInTemplate. You can display the LoginStatus, LoginName of the current logged in user in LoggedInTemplate.
RoleGroups Template consists of collection of templates for different roles stored in the default aspnetdb database that provides the feature of membership as well as roles management. RoleGroups is further divided into asp:RoleGroup that has an attribute to set the Roles to associate the view with any particular role. asp:RoleGroup consists of ContentTemplate that accommodates the set of controls placed inside it to generate the role associated view.
Note: When RoleGroups template is used in LoginView Control then it automatically detects the role of logged in user and displays the role associated view for that role based user. If the current logged in user has not been assigned any role then LoginView displays the LoggedInTemplate view for that user.
Controls placed inside the LoginView control cannot be accessed directly in the code. You have to use FindControl method to find the control placed inside the LoginView control e.g.: LoginView1.FindControl("controlID")
Sample Code for LoginView Control
Following code shows the different views created using all 3 template styles of LoginView Control of ASP.Net 2.0:
<asp:LoginView ID="LoginView1" runat="server"> <RoleGroups> <asp:RoleGroup Roles="admin"> <ContentTemplate> hello admin<br /> <asp:LoginName ID="LoginName1" runat="server" /> </ContentTemplate> </asp:RoleGroup> <asp:RoleGroup Roles="user"> <ContentTemplate> hello user<br /> <asp:LoginName ID="LoginName1" runat="server" /> </ContentTemplate> </asp:RoleGroup> </RoleGroups> <LoggedInTemplate> You are logged in as: <asp:LoginName ID="LoginName1" runat="server" /> </LoggedInTemplate> <AnonymousTemplate> You are not logged in. Please Login... </AnonymousTemplate> </asp:LoginView>
Continue to next tutorial: ASP.Net 2.0 ChangePassword Control to learn how to use the change password control.