How many times you needed to display information which was related to the user’s role? In this article we will see how LoginView control help us to deal with this issue.

Introduction:

How many times you needed to display information which was related to the user’s role? In this article we will see how LoginView control help us to deal with this issue.

The Old Way:

You can always use the old way of doing things but it gets a little hectic. Let’s see how we will display the information to the user who is logged in as Admin.

If(UserInRole(“Admin”)) adminPanel.Visible = true;

If you have multiple roles then you can end up with many if-else checks. In the next section we will look at how to use the LoginView control to solve the problem.

LoginView Control:

The LoginView control is part of the Login controls included in the .NET framework 2.0. The purpose of the control is to give the user a view depending on the user’s role and state. The LoginView control not only checks for the user’s role but can also be customized to display information based on the status of the user. By status we mean that if the person is logged in or is only an anonymous user.

Setting Up The MemberShip Provider:

The first task is to setup the membership provider so that we will be able to use the family of different login controls. Here is the configuration for the membership provider which is placed inside the <system.web> tag of the web.config file.

<membership defaultProvider="SqlMembershipProvider">
        <providers>
          <add name="SqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider"
                connectionStringName="ConnectionString" requiresQuestionAndAnswer="false"
                requiresUniqueEmail="true" minRequiredNonalphanumericCharacters="0" enablePasswordReset="true"
                passwordFormat="Hashed" applicationName="DemoApplication"/>
        </providers>
       
      </membership>

Explanation of the Tags:

defaultProvider: This is the provider which will be picked up by default

In the providers section:

Name: The name of the provider.
Type: The type of the provider. You are free to create your own custom provider.
connectionStringName: The name of the connectionString used to connect to the database.
requiresQuestionAndAnswer: Determines if the user needs question and answer to register as a user.
requiresUniqueEmail: Determines if the email needs to be unique.
minRequiredNonalphanumericCharacters: A property which ensures the strength of the password.
enablePasswordReset: Determines if password can be reset.
passwordFormat: Determines how the password is stored in the database. It is a good idea to use the hashed password.

Setting Up the LoginView Control:

Now, it is time to set up the LoginView control. Let’s check out the markup below:

<asp:LoginView ID="loginView1" runat="server">
 
 
 <LoggedInTemplate>
 
 You are viewing the logged in template!
 
 </LoggedInTemplate>
 
 <AnonymousTemplate>
 
 You are viewing the anonymous template!
 
 </AnonymousTemplate>

 </asp:LoginView>

In the above code we have declared two templates of the LoginView control, the LoggedInTemplate and the AnonymousTemplate. As, the name suggest the LoggedInTemplate is displayed when the user is successfully logged in. The AnonymousTemplate is displayed when the user is not logged in.

Simple right! 

Now, let’s see how to enables roles on the LoginView control. The first task is to enable roles for the membership provider. Check out the following configuration setting in the web.config file.

<roleManager enabled="true" cacheRolesInCookie="true" cookieName="RCRoleCookie"
      defaultProvider="RCRoleProvider">
        <providers>
          <add connectionStringName="ConnectionString" applicationName="DemoApplication"
             name="RCRoleProvider" type="System.Web.Security.SqlRoleProvider" />
        </providers>
      </roleManager>

The above configuration enable roles for the membership provider. Now, let’s see how LoginView control can be customized to use roles.

<LoggedInTemplate>
 
 You are viewing the logged in template!
 
 </LoggedInTemplate>
 
 <AnonymousTemplate>
 
 You are viewing the anonymous template!
 
 </AnonymousTemplate>
 
 
 <RoleGroups>
 <asp:RoleGroup Roles="Admin">
 <ContentTemplate>
 You are viewing the Admin
 </ContentTemplate>
 </asp:RoleGroup>
 
 <asp:RoleGroup Roles="Member">
 <ContentTemplate>
 You are viewing the Member Template!
 </ContentTemplate>
 </asp:RoleGroup>
 
 </RoleGroups>
 
 
 </asp:LoginView>

The <RoleGroups> section is used to setup the roles. Inside the section we have declared the <RoleGroup> element which has the Roles attribute. The <ContentTemplate> is used to display the custom information which is related to roles. From the above configuration you can see that if the user is logged in as “Admin” then the text “You are viewing the Admin” is displayed.

Conclusion:

In this article we learned how to use the LoginView control to display a custom view to the user. The view can be customized using LoginView templates.