SidD
SidD

Reputation: 6377

Find all roles in context.user

i have to check all the roles for a user.currently im checking isinrole. but i want to find all roles.to hide show some data.

 if (Context.User.IsInRole("Travel_Admin"))
                    {
                        visibleTables.Remove(item_level10);
                        visibleTables.Add(item_level1);
                        visibleTables.Add(item_level2);
                        visibleTables.Add(item_level3);
                        visibleTables.Add(item_level4);
                        visibleTables.Add(item_level5);
                    }
                    else
                    {
                        visibleTables.Remove(item_level1);
                        visibleTables.Remove(item_level2);
                        visibleTables.Remove(item_level3);
                        visibleTables.Remove(item_level4);
                        visibleTables.Remove(item_level5);
                        visibleTables.Remove(item_level12);
                        visibleTables.Remove(item_level10);
                    }

I need to find all roles in context.user for that specific user. Note:I am beginner in role based authentication.

Upvotes: 1

Views: 3089

Answers (2)

Nicolas R
Nicolas R

Reputation: 14609

If you need to find all the roles of your user, use the following:

Roles.GetRolesForUser()

It will give you a string array of all its roles. You can specify a user for the mtehod.

You can use it in a method like:

foreach(string role in Roles.GetRolesForUser())
{
   // do treatment for this role of the user
}

See doc here for this method: http://msdn.microsoft.com/en-us/library/system.web.security.roles.getrolesforuser.aspx

In MX2 solution, you browse all types of roles defined, not only this user roles!

You can find Roles methods here in MSDN: http://msdn.microsoft.com/en-us/library/System.Web.Security.Roles_methods.aspx

Upvotes: 2

MX2
MX2

Reputation: 28

Try this:

 foreach (var item in Roles.GetAllRoles())
 {
      if(Context.User.IsInRole(item))
      {
            //code
      }
      else
      {
            //code
      }
 }

Upvotes: 0

Related Questions