It can be used to show or hide a group of control or content
based on the user login status.
Below is the syntax of using it.
Listing 9 - LoginView Control
<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>
</LoggedInTemplate>
<AnonymousTemplate>
</AnonymousTemplate>
</asp:LoginView>
Things inside <LoggedInTemplate> will be exposed only
if the user is already logged in where the things in <AnonymousTemplate>
will be visible even if the user is not logged in.
For example, if we want to display or hide controls based on
the user's role then this control is a champion of doing it with very little
effort. Consider we have an error page that reports the error to the users. If
we want to hide the technical error message to all the users and show the
technical error message only for the users with ADMIN role then we can use
LoginView control to achieve this requirement.
Refer to the below code for the implementation.
Listing 10 - LoginView Control With Roles
<asp:LoginView ID="loginview" runat="server">
<RoleGroups>
<asp:RoleGroup Roles="ADMIN">
<ContentTemplate>
<b>Technical Information:</b><%= TechnicalErrorMsg %>
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
The above code will display the technical error messages
only to the users with ADMIN Role.
We can also achieve the above with the following code.
Listing 11 - Role Chech In Code
if (User.IsInRole("ADMIN"))
lblMessage.Text = TechnicalErrorMsg;
The next sections will describe some of the common problems
that we will come across when we use FormsAuthentication and the workarounds to
those problems.