public ActionResult CreateUser(CreateUserViewModel createUserViewModel) { if (!ModelState.IsValid) return View(createUserViewModel); try { if(createUserViewModel.Password != createUserViewModel.ConfirmPassword) throw new MembershipCreateUserException("Passwords do not match."); var user = _userService.Create( createUserViewModel.Username, createUserViewModel.Password, createUserViewModel.Email, createUserViewModel.PasswordQuestion, createUserViewModel.PasswordAnswer, true); if (createUserViewModel.InitialRoles != null) { var rolesToAddUserTo = createUserViewModel.InitialRoles.Where(x => x.Value).Select(x => x.Key); foreach (var role in rolesToAddUserTo) _rolesService.AddToRole(user, role); } return RedirectToAction("Details", new { id = user.ProviderUserKey }); } catch (MembershipCreateUserException e) { ModelState.AddModelError(string.Empty, e.Message); return View(createUserViewModel); } }
public ViewResult CreateUser() { var model = new CreateUserViewModel { InitialRoles = _rolesService.FindAll().ToDictionary(k => k, v => false) }; return View(model); }