开发者

mvc 3 membership Role.AddUserToRole

i have array for role selected. and now i want to add that array role to specified user. i have a controller like this :

string[] array = collection["RolesSelected"].Split(',');
        try
        {
            Roles.AddUserToRoles(username, array);

        }

but when i submit, nothing happens. when i debug in

 Roles.AddUserToRoles(username, array);

it shows correct. i mean username is correct, and array is correct(list of role that checked).

if i try to re开发者_如何学JAVAmove role,

Roles.RemoveUserFromRoles(username,array)

the role for username is removed.

why did i can remove role but i cant add role ?

thanks

ps:sorry for my english :)

EDIT

i can remove role for specified user using

Roles.RemoveUserFromRoles(username,array)

but i cant add role to specified user. when i remove all roles from specified user, then i can add role for user. should i remove all roles first, and then adding them again (i think i cant do that because i have many users and roles for my project)


I suspect that you are encountering an error (System.Configuration.Provider.ProviderException is being thrown) by trying to add a user to a role in which s/he already exists. Try this instead:

string[] array = collection["RolesSelected"].Split(',');
foreach (string role in array)
{
   if (!Roles.IsUserInRole(username, role))
   {
      Roles.AddUserToRole(username, role);
   }
}

That said, if you want the user to only have the roles specified in the array, you'd want to remove all the roles first and then do the AddUserToRole call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜