CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
LoginView Controls with Roles in ASP.Net 2.0

By Satheesh babu
Posted On June 04,2008
Article Rating:
Be first to rate
this article.
No of Comments: 3
Category: ASP.Net
Print this article.

LoginView Controls with Roles in ASP.Net 2.0

Authentication and authorization is one of the most important modules when we develop any asp.net applications. Forms Authentication is one of the most widely used authentication methods used in asp.net. We will assign roles to the users so that a proper access rights can be given to access the resources on the application, in other words called authorization. Read my article on “Role based Forms authentication in ASP.Net 2.0” provided in the reference section of this article.

Traditionally, we will restrict or allow access to a resource by checking the role of the logged in user in code. Refer the below code for better understanding.

if (User.IsInRole("ADMIN"))

btnUpdate.Visible = true;

 

In the above approach, we will be repeating the code to restrict or allow access to a resource on every page. With the introduction ASP.net 2.0, we have a set of new controls called Login controls which will work with providers to provide authentication and authorization to asp.net application with less effort. Among the Login controls in ASP.net 2.0, LoginView control will be very much useful when we use roles with forms authentication. Use of LoginView control will help us to show or hide controls based on the user roles. Also, with LoginView controls we can show or hide controls based on the user’s authenticated status i.e. we can show a set of controls to the user only when he is logged in. In this article we will discuss the usage of LoginView controls and its powerfulness when we use roles.

 

LoginView Controls

 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.

<asp:LoginView ID="LoginView1" runat="server">

 <LoggedInTemplate>

</LoggedInTemplate>

<AnonymousTemplate>

</AnonymousTemplate>

</asp:LoginView>

 

If you see the above syntax, Login view control will have 2 templates,

·          LoggedInTemplate

·          AnonymousTemplate

 




Things inside LoggedInTemplate will be shown only if the user is logged in where AnonymousTemplate will be visible if the user is not logged in.

Example

<asp:LoginView ID="loginvew" runat="server">

<AnonymousTemplate>

You are not Logged in.

</AnonymousTemplate>

<LoggedInTemplate>

  You are Logged in.<asp:LoginStatus ID="LoginStatus2" runat="server" />

</LoggedInTemplate>

</asp:LoginView>

 

In the above example, when the user is not logged in then it will show the text “You are not Logged in.” and when the user is logged in it will show “You are Logged in” with a hyperlink as “Logout” to logout the session.

 

Using LoginView Control with Roles

The most cool feature of this control is using it based on Roles. Suppose, if we want to display some controls based on the user role then this control is a champion of doing it with very less effort.

 

Refer the below code for using it.

 

<asp:LoginView ID="loginvew" runat="server">

    <RoleGroups>

    <asp:RoleGroup Roles="Admin">

    <ContentTemplate>

<asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Update" />

    </ContentTemplate>

    </asp:RoleGroup>

    </RoleGroups>

   </asp:LoginView>

 

The above code will show the update button only if the logged user is associated with the role “Admin”.

If we are using the LoggedInTemplate and RoleGroups simultaneously in LoginView control then LoggedInTemplate will show the controls/contents only if the user does not belong to any role group present in the Rolegroups tag.

Reference

Role based Forms Authentication in ASP.Net 2.0

 

Conclusion

Thus LoginView control helps us to prevent the custom code we write to enable and hide controls based on the user’s role. Consider reading my article about “Role based Forms Authentication in ASP.Net 2.0” given in Reference section.

 

Happy Coding!!!

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
Dynamic LoginView
Is there a way to accomplish this in a dynamic way? What I mean and taking for reference your example is that by code behind I could change role name from “Admin” to “Consultor” or also that this section “ <asp:RoleGroup” could have two groups related not just one
Thanks
Nice one
Rendered nicely... Hats off..
loginview
its very useful for me. thanks a lot