Client Application Services - Part 2
page 5 of 10
by Bilal Haidar
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 17439/ 216

Authorization

In this section we will retrieve some information related to the roles the user belongs to. Another method call is added to the Form_Load which is DisplayUserRoles. This method shall retrieve the roles that the user belongs to, if any, and bind the data to a DataGridView placed on the main form.

Listing 6

private void DisplayUserRoles()
{
  // Get the ClientRolePrincipal
  ClientRolePrincipal principal = (ClientRolePrincipal)
    System.Threading.Thread.CurrentPrincipal;
  // Get an instance of the ClientRoleProvider
  ClientRoleProvider roleProvider = (ClientRoleProvider)Roles.Provider;
 
  // Get user roles
  string[]roles = roleProvider.GetRolesForUser(principal.Identity.Name);
 
  panel1.Visible = roles.Length >= 1;
  if (panel1.Visible)
  {
    foreach (string s in roles)
      this.dataGridView1.Rows.Add(s);
  }
}

First of all, the code retrieves an instance of the ClientRolePrincipal from the CurrentPrincipal object on the System.Threading.Thread object. There is nothing special about this object except a method called IsInRole which accesses internally the ClientRoleProvider.IsUserInRole method.

Once the ClientRolePrincipal object is created, an instance of the ClientRoleProvider is cast from the Roles.Provider property. You could have directly used the Roles.Provider property to access the methods related to roles, but using an instance of ClientRoleProvider is better since it will show you which methods are implemented and which are not.

As mentioned in part 1 of this series, the role management in Client Application Services is read-only, you can only check whether the user is in a specific role or not by using the IsUserInRole method and another method to retrieve user roles called GetRolesForUser.

A call for the GetRolesForUser is issued to retrieve the user’s roles. If there are any roles for the currently logged in user, they are bound to a DataGridView on the main form.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 6 and 2 and type the answer here:

User Comments

Title: Good Article   
Name: Adron
Date: 10/20/2008 12:07:25 AM
Comment:
Good write up. I'm working through multiple scenarios right now with this, so it is interesting to read and helpful.

Thx.

Product Spotlight
Product Spotlight 






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


©Copyright 1998-2009 ASPAlliance.com  |  Page Processed at 11/21/2009 2:20:05 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search