How do you rename a role using .NET membership?

I use ASP.NET membership and noticed that in the role class there is no way to change the role (its name for example), only to create and delete them.

Is this possible or not supported?

EDIT: @CheGueVerra: Yes, a good workaround.

Do you know (for extra credit :)) why this is not possible?

+6
security asp.net-membership roles
source share
2 answers

There is no direct way to change the role name in a membership provider.

I would get a list of users who are in the role that you want to rename, then remove from the list, delete the role, create a role with a new name, and then add the users that were discovered earlier to the role using the new name.

public void RenameRoleAndUsers(string OldRoleName, string NewRoleName) { string[] users = Roles.GetUsersInRole(OldRoleName); Roles.CreateRole(NewRoleName); Roles.AddUsersToRole(users, NewRoleName); Roles.RemoveUsersFromRole(users, OldRoleName); Roles.DeleteRole(OldRoleName); } 

This will change the role name for all users in the role.

Follow-up: the roles used to ensure that the user plays only his role in the system, in this way User.IsInRole (ROLE_NAME) will help you enforce BR securities for the user and the role he is in. If you can change the role names on the fly, how are you going to verify that the user is really in this role. It’s good that I understood when I asked about this.

rtpHarry edit: Converted pseudocode example to compiled C # method

+19
source share

Renaming a role in the ASP.NET membership model will be programmatic - this is Bad Thing & trade;, because role names are used in the configuration file to determine permissions. If there was a programmatic way to change the role name (which saved the change in the database), you would immediately violate any role-based security configurations in web.config for any web applications using the database, and there would be no way to guarantee that one web application can change the configuration of each web application using this membership database.

+5
source share

All Articles