Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net is user in role

Tags:

c#

asp.net

roles

I'm having trouble working out how to quickly find out yes/no is a username in a role? I've gotten as far as:

Roles.FindUsersInRole("Admin", usersName)

But am a bit stuck, any easy way of doing this?

like image 241
Tom Gullen Avatar asked Nov 22 '10 10:11

Tom Gullen


People also ask

What is user and role?

Definition of user-defined user rolesA role is a database object that groups together one or more privileges and can be assigned to users. A user that is assigned a role receives all of the privileges of that role. A user can have multiple roles. A role hierarchy is also supported.

What is ASP NET role?

ASP.NET offers a Roles framework for defining roles and associating them with user accounts. With the Roles framework we can create and delete roles, add users to or remove users from a role, determine the set of users that belong to a particular role, and tell whether a user belongs to a particular role.

What can you use to identify whether an authenticated user is a member of a role?

The RolePrincipal object's IsInRole(roleName) method calls Roles. GetRolesForUser to get the roles for the user in order to determine whether the user is a member of roleName. When using the SqlRoleProvider , this results in a query to the role store database.


1 Answers

The below returns true or false depending on if the specified user is in the specified role

Roles.IsUserInRole(userName, role)

So, for example, if you wanted to remove a user from a specific role you could use

 if (Roles.IsUserInRole(userName, role))
         Roles.RemoveUserFromRole(userName, role);
like image 166
Matt Avatar answered Sep 21 '22 07:09

Matt