예제 #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();
            }
        }
예제 #2
0
 protected static void Seed(DbEntitiesContext context)
 {
     PerformInitialSetup(context);
     //base.Seed(context);
 }
예제 #3
0
 public UnitOfWork(DbEntitiesContext context)
 {
     this.context = context;
 }