public ActionResult Create(CreateUserRoleViewModel userRoleModel) { var model = userRoleModel.UserModel; var selectedRoles = userRoleModel.Roles; if (ModelState.IsValid) { // Attempt to register the user MembershipCreateStatus createStatus; var user = MembershipUserExtended.CreateUser(model.UserName, model.Password, model.Email, model.FirstName, model.LastName, model.Title, model.HourlyRate, model.Address, model.City, model.State, model.Zip, model.Phone, out createStatus); if (createStatus == MembershipCreateStatus.Success) { //Assign Users to Roles if (selectedRoles.Any()) Roles.AddUserToRoles(user.UserName, selectedRoles.ToArray()); else Roles.AddUserToRole(user.UserName, Roles.GetAllRoles().FirstOrDefault(c => c.ToLower().Equals("user"))); userRoleModel.UserModel = new CreateUserModel(user); userRoleModel.Roles = Roles.GetAllRoles().ToList(); //FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false); //return RedirectToAction("Index", "TimeTrack"); ModelState.AddModelError("", string.Format("User {0} created successfully",model.UserName)); return View(userRoleModel); } else { userRoleModel.Roles = Roles.GetAllRoles().ToList(); ModelState.AddModelError("", ErrorCodeToString(createStatus)); } } else { userRoleModel.Roles = Roles.GetAllRoles().ToList(); } // If we got this far, something failed, redisplay form return View(userRoleModel); }
// // GET: /User/ // // GET: /Account/Register //[Authorize(Roles = "SuperAdmin, Admin")] public ActionResult Create() { var model = new CreateUserRoleViewModel(); return View(model); }