Role Based Forms Authentication in ASP.NET 2.0
page 10 of 16
by Satheesh Babu
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 68515/ 130

Using LoginView Control with Roles

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">

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">
    <asp:RoleGroup Roles="ADMIN">
        <b>Technical Information:</b><%= TechnicalErrorMsg %>

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.

View Entire Article

User Comments

Title: it works at once   
Name: snopbear
Date: 2008-08-26 9:32:07 AM
When I log in with test1 or any of them, I get redirected to a HTTP Error 404 - Not Found page. Any ideas?
Title: Question   
Name: Jeff
Date: 2008-06-26 9:53:38 AM
When you log into the site with a user, go to their homepage, then click logout, hit the BACK button enough, it will get you into the home page without credentials.

Once you hit refresh it kicks you out and you have to login again.

This happens in IE6. It seems to work in Firefox right.
Title: Something is wrong   
Name: Mike
Date: 2008-06-25 10:16:43 AM
When I log in with test1 or any of them, I get redirected to a HTTP Error 404 - Not Found page. Any ideas?
Title: permissions and roles   
Name: tariq
Date: 2008-05-12 5:39:03 AM

I need to grant users with permissions depending on their roles. I have heard about Visual Guard .Net before I start testing, do you have any feedback about this tool?

thank you
Title: Thnx   
Name: Dhaval Patel
Date: 2008-05-02 11:04:11 AM
thanks buddy... it helped understanding it better.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2023  |  Page Processed at 2023-10-04 12:41:02 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search