public bool CreateNewAdmin(AdminUserCreateRAO user) { if (user.Password != user.ConfirmPassword) { return(false); } var entity = new ApplicationUser { UserName = user.UserName, Email = user.Email }; using (var ctx = new ApplicationDbContext()) { if (ctx.Users.Where(u => u.Email == user.Email || u.UserName == user.UserName).Count() == 0) { var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(ctx)); userManager.Create(entity, user.Password); var newAdmin = ctx.Users.FirstOrDefault(u => u.Email == user.Email); userManager.AddToRole(newAdmin.Id, "Admin"); return(true); } else { return(false); } } }
public ActionResult AdminCreate(AdminUserCreateDTO user) { if (!ModelState.IsValid) { return(View(user)); } var service = CreateAdminService(); if (!service.IsAdminUser()) { return(RedirectToAction("Index", "Home")); } var rao = new AdminUserCreateRAO { UserName = user.UserName, Email = user.Email, Password = user.Password, ConfirmPassword = user.ConfirmPassword }; if (!service.CreateNewAdmin(rao)) { return(View(user)); } return(RedirectToAction("Index")); }