public ActionResult Register(RegisterViewModel Model) { if (ModelState.IsValid) { // Attempt to register the user try { string confirmationToken = WebSecurity.CreateUserAndAccount( Model.UserName, Model.Password, new { Email = Model.Email }, true); Roles.AddUserToRole(Model.UserName, "User"); userMailer.RegisterConfirmation(Model.Email, Model.UserName, confirmationToken).Send(); logger.Info("Send register confirm email to \"" + Model.UserName + "\""); return(RedirectToAction("RegisterStepTwo", "Account")); } catch (MembershipCreateUserException ex) { logger.Warn("Registration failed with error: \"" + ErrorCodeToString(ex.StatusCode) + "\""); ModelState.AddModelError("", ErrorCodeToString(ex.StatusCode)); } } // If we got this far, something failed, redisplay form return(View(Model)); }
public ActionResult CreateUser(viewModels.RegisterViewModel Model) { if (ModelState.IsValid) { // Attempt to register the user try { string confirmationToken = null; if (Model.Approve) { confirmationToken = WebSecurity.CreateUserAndAccount( Model.UserName, Model.Password, new { Email = Model.Email, CreatedByUserID = WebSecurity.CurrentUserId }, true); userMailer.RegisterConfirmation(Model.Email, Model.UserName, confirmationToken).Send(); } else { WebSecurity.CreateUserAndAccount(Model.UserName, Model.Password, new { Email = Model.Email, CreatedByUserID = WebSecurity.CurrentUserId }); } Roles.AddUserToRole(Model.UserName, "User"); logger.Info("User " + WebSecurity.GetUserId(User.Identity.Name) + " \"" + User.Identity.Name + "\" created new account \"" + Model.UserName + "\""); return(RedirectToAction("GrantRolesToUsers", new { UserID = WebSecurity.GetUserId(Model.UserName) })); } catch (MembershipCreateUserException ex) { logger.Warn("Error occured on user " + WebSecurity.GetUserId(User.Identity.Name) + " \"" + User.Identity.Name + "\" account creating: \"" + ErrorCodeToString(ex.StatusCode) + "\""); ModelState.AddModelError("", ErrorCodeToString(ex.StatusCode)); } } return(View(Model)); }