I created a new ASP.NET MVC-5 application with Individual User Accounts and then updated all the Nuget packages in the solution. Now I'm trying to fulfill some of the recommendations shown in some tutorials, but I ran into some problems. The first is that the ApplicationRoleManager class, which is used throughout the application, was not created ( ApplicationUserManager was created). The second problem is more about Entity-Framework : I saw that for planting a database using a user and a role, many people create a static constructor in the ApplicationDbContext class:
static ApplicationDbContext() { Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer()); }
So, I added it, and the implementation of ApplicationDbInitializer :
public class ApplicationDbInitializer : DropCreateDatabaseIfModelChanges<ApplicationDbContext> { protected override void Seed(ApplicationDbContext context) { InitializeIdentityForEF(context); base.Seed(context); } //Create User=Admin@Admin.com with password=Admin@123456 in the Admin role public static void InitializeIdentityForEF(ApplicationDbContext db) { var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>(); var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>(); const string name = " admin@admin.com "; const string password = " Admin@123456 "; const string roleName = "Admin"; //Create Role Admin if it does not exist var role = roleManager.FindByName(roleName); if (role == null) { role = new IdentityRole(roleName); var roleresult = roleManager.Create(role); } var user = userManager.FindByName(name); if (user == null) { user = new ApplicationUser { UserName = name, Email = name }; var result = userManager.Create(user, password); result = userManager.SetLockoutEnabled(user.Id, false); } // Add user admin to Role Admin if not already added var rolesForUser = userManager.GetRoles(user.Id); if (!rolesForUser.Contains(role.Name)) { var result = userManager.AddToRole(user.Id, role.Name); } }
After adding everything, I opened the Package Manager Console and typed Enable-Migrations , then Add-Migration someName , and then Update-Database . the results were that the database was created successfully, but the data was not inserted into the database.
After you noticed that the data was not inserted, I moved the Seed logic to the Index method of the home controller, and the data was inserted after the application started. I also needed to add this line: app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create); to the file Startup.Auth.cs .
So my questions are:
- Do I need to enter the
ApplicationRoleManager class manually? - How to create a
Seed method?
UPDATE
I changed the Seed method to:
protected override void Seed(ApplicationDbContext context) { var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>(); //since there is no ApplicationRoleManager (why is that?) this is how i create it var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)); const string name = " admin@admin.com "; const string password = " Admin@123456 "; const string roleName = "Admin"; //Create Role Admin if it does not exist var role = roleManager.FindByName(roleName); if (role == null) { role = new IdentityRole(roleName); var roleresult = roleManager.Create(role); } //app hangs here... var user = userManager.FindByName(name); if (user == null) { user = new ApplicationUser { UserName = name, Email = name }; var result = userManager.Create(user, password); result = userManager.SetLockoutEnabled(user.Id, false); } // Add user admin to Role Admin if not already added var rolesForUser = userManager.GetRoles(user.Id); if (!rolesForUser.Contains(role.Name)) { var result = userManager.AddToRole(user.Id, role.Name); } base.Seed(context); }
So, now the administrator role is created, but when accessing var user = userManager.FindByName(name); the application freezes without any exception or any message ...