ASP.NET MVC 5 Identity AddUserToRole not works

I'm trying to insert initial users, roles and attach users to roles using ASP.NET MVC 5 Identity.

When Configuration.AutoDetectChangesEnabled = false for DbContext, which was used for UserManager and RolesManager,

code userManager.AddUserToRole(user, "TestRole") returns success, but nothing was updated in db.

Is there some way to solve this problem without creating own UserManager implementation and UserStorage implementation?

Answers


I'm using Migrations and in my Seed method I create roles this way:

private void CreateRole(string roleName)
{
    var rm = new RoleManager<IdentityRole>(
    new RoleStore<IdentityRole>(new AppDbContext()));
    var idResult = rm.Create(new IdentityRole(roleName));
}

And to add the role to the user:

var userStore = new UserStore<AppUser>(new AppDbContext());
var manager = new UserManager<AppUser>(userStore);
var user = manager.FindByName(userName);
if ( user == null ){
   manager.Create(new AppUser { UserName = userName }, password);
}

user = manager.FindByName(userName);
manager.AddToRole(user.Id, roleName);

And it updates the database properly.


My Environment: Project Scaffolded with Individual User Accounts selected. It is using ASP.net MVC5, Identity 1.0 I enabled Code-First Migrations on my PrimaryApplicationContext (Not the ApplicationDbContext that is created in IdentityModel.cs)

My Seed Method looks as follows

protected override void Seed(MYProject.DAL.PrimaryApplicationContext context)
        {
            ApplicationDbContext userContext = new ApplicationDbContext();
            var rm = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(userContext));
            var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(userContext));

            if (!rm.RoleExists("Developer"))
            {
                var idResult = rm.Create(new IdentityRole("Developer"));
            }

            if (um.FindByName("Super") == null)
            {
                um.Create(new ApplicationUser() { UserName = "Super" } , "Password");
                ApplicationUser user = um.FindByName("Super");
                um.AddToRole(user.Id, "Developer"); 
            }

        }

If your application uses the same context to store user data as it does application data, you would not need to create a new ApplicationDbContext, you could just use the context passed into the seed method.

step 1- Create an instance of whichever context is used to store identity data.

step 2- Create instances of RoleManager, and UserManager with step 1 context. *If you encounter an error like "Role Manager is not enabled", you need to add

<roleManager enabled="true" /> To your Web.config, under the node.

step 3- Use the role manager to see if the role already exists, if not- create it.

step 4- use the User manager to see if the user already exists, if not- create it.

step 5- Once the role and the user are created, retrieve the user from the database (You will need the user id to add to the role) *Be aware the user, and the role managers Create methods return an Identity Result object, not a role/user object, so the following code is INVALID

var result = usermanager.Create(new ApplicationUser()...
usermanager.AddToRole( result.Id, "Developer");

step 6- use the UserManager to add the retrieved user to the role. step 7- Issue an Update-Database command to the Package-Manager-Console to re-seed the database.


Need Your Help

Is there a way to optimize the way I generate a file and output it as .zip in a java servlet?

java optimization memory servlets file-io

A user has a few Lookup selections on a JSP page. After he's selected what data he wants exported, the JSP calls my servlet which should do something like this:

How to Add a New Column With Selected Values from Another Column In Python

python pandas dataframe

I have been trying to figure this out all day. I am new to Python.