I spent past two days digging throughout the internet trying to create an action under one of the controllers that would take a parameter: user id and this user to a particular group. I have created the groups directly in the database.

There are multiple articles about this including this one http://typecastexception.com/post/2014/06/22/ASPNET-Identity-20-Customizing-Users-and-Roles.aspx, but they all are very confusing.

What I am looking for is ideally a one liner like

System.Web.Security.Roles.AddUserToRole("5b1f5163-84b7-4987-9dba-ee809c0e5bc2", "AcceptedUsers");

If I execute this code I get an error saying that "System.Configuration.Provider.ProviderException: The role '' was not found." Why is it not seeing the role name?

How difficult could it be to add a user to a group? This is a feature that all developers are certainly looking for.

What I have managed to do is add all newly registered users to a default group like this:

var context = new ApplicationDbContext();
            var roleStore = new RoleStore<IdentityRole>(context);
            var roleManager = new RoleManager<IdentityRole>(roleStore);

            var userStore = new UserStore<ApplicationUser>(context);
            var userManager = new UserManager<ApplicationUser>(userStore);
            userManager.AddToRole(user.Id, "NotAcceptedYet");

,although if I try to call this block in a different action it doesn't seem to be adding/removing users from roles.

Have any one have a tested solution to do this? I am really surprised that description of this doesn't come as a part of language documentation.

Related posts

Recent Viewed