Beispiel #1
0
        public static async Task <bool> Initialize(ApplicationDbContext context, IUserService userService)
        {
            context.Database.EnsureCreated();
            if (!context.Role.Any())
            {
                context.Role.Add(new Role {
                    Id = "1", Name = "Administrator"
                });
                context.Role.Add(new Role {
                    Id = "2", Name = "Manager"
                });
                context.Role.Add(new Role {
                    Id = "3", Name = "Admin"
                });
                context.Role.Add(new Role {
                    Id = "4", Name = "Accounting"
                });
                context.Role.Add(new Role {
                    Id = "5", Name = "Operational"
                });
                context.Role.Add(new Role {
                    Id = "6", Name = "Employee"
                });
                context.SaveChanges();
            }


            if (!context.Users.Any())
            {
                try
                {
                    var administratorModel = new Models.RegisterModel {
                        UserName = "******", Password = "******"
                    };
                    var administrator = await userService.Register(administratorModel);

                    await userService.AddToRole(administrator, "Administrator");
                }
                catch (System.Exception ex)
                {
                    throw new System.Exception(ex.Message);
                }
            }

            return(true);
        }
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                Membership.CreateUser(model.UserName, model.Password, model.Email, passwordQuestion: null, passwordAnswer: null, isApproved: true, providerUserKey: null, status: out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public ActionResult JsonRegister(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                Membership.CreateUser(model.UserName, model.Password, model.Email, passwordQuestion: null, passwordAnswer: null, isApproved: true, providerUserKey: null, status: out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
                    return Json(new { success = true });
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed
            return Json(new { errors = GetErrorsFromModelState() });
        }