Example #1
0
        protected override void Seed(ApplicationDbContext context)
        {
            ApplicationRoleManager roleManager = HttpContext.Current.GetOwinContext().Get <ApplicationRoleManager>();
            ApplicationUserManager userManager = HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>();

            roleManager.GetOrCreateRole(IdentityHelper.RoleEmployee);

            var roleAdmin = roleManager.GetOrCreateRole(IdentityHelper.RoleAdmin);
            var userAdmin = userManager.FindByName(Properties.Settings.Default.AdminUserName);

            if (userAdmin == null)
            {
                userAdmin = new ApplicationUser {
                    UserName = Properties.Settings.Default.AdminUserName, Email = Properties.Settings.Default.AdminUserName
                };
                userManager.Create(userAdmin, Properties.Settings.Default.AdminPassword);
                userManager.SetLockoutEnabled(userAdmin.Id, false);
            }

            var rolesForUser = userManager.GetRoles(userAdmin.Id);

            if (!rolesForUser.Contains(roleAdmin.Name))
            {
                userManager.AddToRole(userAdmin.Id, roleAdmin.Name);
            }
        }