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.
private void DisplayUserRoles()
// Get the ClientRolePrincipal
ClientRolePrincipal principal = (ClientRolePrincipal)
// Get an instance of the ClientRoleProvider
ClientRoleProvider roleProvider = (ClientRoleProvider)Roles.Provider;
// Get user roles
stringroles = roleProvider.GetRolesForUser(principal.Identity.Name);
panel1.Visible = roles.Length >= 1;
foreach (string s in roles)
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
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.