views:

651

answers:

2

I'm using ASP.NET Membership and noticed there isn't a method in the Roles class to modify a role (its name for instance), only to create and delete them.

Is it possible or it's not supported?

EDIT: @CheGueVerra: Yes, nice workaround.

Do you know (for extra credit :) ) why it's not possible?

+5  A: 
Harper Shelby
true, but I don't plan on using the configuration file, as new roles can be created by my users, and they can configure through the application which pages the new roles have access to. I need to add a runtime check, so this restriction is not necessary
Juan Manuel
And wouldn't the same happen if you delete a role?
Juan Manuel
+4  A: 

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

I would get the list of users that are in the role you want to rename, then remove them from the list, delete the role, create the role with the new name and then Add the users found earlier to the role with the new name.

string[] users = GetUsersInRole(YOU_OLD_ROLE);
RemoveUsersFromRoles(users, YOUR_OLD_ROLE);
DeleteRole(YOUR_OLD_ROLE);
CreateRole(YOUR_NEW_ROLE);
AddUsersToRoles(users, YOUR_NEW_ROLE);

That will change the name of the role for all users in the role.

Follow-up: Roles, are used to ensure a user plays only his part in the system, thus User.IsInRole(ROLE_NAME), will help you enforce the BR securities that apply, for a user and the roles he is in. If you can change the role names on the fly, how are you going to validate that the user is really in that role. Well that's what I understood, when I asked about it.

CheGueVerra