Example #1
0
        public static void PerformInitialSetup(DbEntitiesContext context)
        {
            using (var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)))
            {
                using (var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)))
                {
                    //system Role
                    if (!roleManager.RoleExists("admin"))
                    {
                        roleManager.Create(new IdentityRole("admin"));
                    }
                    if (!roleManager.RoleExists("normal user"))
                    {
                        roleManager.Create(new IdentityRole("normal user"));
                    }
                }
                var user = new ApplicationUser()
                {
                    UserName = "******", Email = "*****@*****.**", PhoneNumber = "00000000000"
                };

                //IdentityResult userResult = userManager.Create(user, "123456");

                //if (userResult != IdentityResult.Success)
                //    throw new Exception("failed");

                if (userManager.FindByEmail(user.Email)?.Email != "*****@*****.**")
                {
                    if (userManager.Create(user, "1234Admin!") != IdentityResult.Success)
                    {
                        throw new Exception("failed");
                    }
                    // role
                    userManager.AddToRole(user.Id, "admin");
                }

                context.SaveChanges();
            }
        }
Example #2
0
 public void Save()
 {
     context.SaveChanges();
 }