I use ASP.NET Identity 2.0 in my MVC application and I want to assign 2 default roles to the user who login to the system for the first time by adding 2 records to the AspNetUserRoles table with UserId and RoleId. Is there a practical way to to this? Or do I have to add these default roles using DBContext and Entity Framework, etc. (I use EF Code First)? Any help would be appreciated. 
After creating the user record in Register post action
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
You can add the roles
await UserManager.AddToRoleAsync(user.Id, "role1");
await UserManager.AddToRoleAsync(user.Id, "role2");
                        You could do a check in the AccountController, Register method (the one with the HttpPost), something like: if (!MyDbContext.Users.Any()) {...}  
And if there are no users, assign roles to the newly created user with: UserManager.AddToRoleAsync
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With