public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { // if this succeeds we want to create the User Details obj ect EVery time a new user registers //create user detail object UserDetail newUserDetail = new UserDetail() { UserId = user.Id, FirstName = model.FirstName, LastName = model.LastName, FavoriteColor = model.FavoriteColor }; //Establish a connection to the db BookStorePlusEntities db = new BookStorePlusEntities(); //send to entity framework db.UserDetails.Add(newUserDetail); db.SaveChanges(); #region Add the new user to a role //If you are going to allow user to register, you should default them into a role. //Default them to a Customer Role //Create customer role UserManager.AddToRole(user.Id, "Customer"); //Every person who uses the register view gets assigned the customer role #endregion return(View("Login")); // BELOW WAS AN EMAIL CONFIRMATION DEMO. WE'RE NOT USING IT //var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); //await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>"); //ViewBag.Link = callbackUrl; //return View("DisplayEmail"); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { #region Code for Verify Email address - Default - Not implemented //var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); //await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking this link: <a href=\"" + // callbackUrl + "\">link</a>"); //ViewBag.Link = callbackUrl; //return View("DisplayEmail"); #endregion //make sure the user (newly created) gets added to a valid role. //- (make sure this role exists via the ManageRoles Link) UserManager.AddToRole(user.Id, "Customer"); /* * This ONLY creates a new user when using THIS action/View (register) * If you want this functionality when creating users via the UsersAdmin Controller * the logic below needs to be added to that Action/View. You will also need to update * the Edit Action/View - AS WELL AS EditUserViewModel in the AdminViewModel Class (models folder), * * For Deleting a user (via the UsersAdmin Controller) you would also need to get the UserDetail object * associated with the user and delete it as well. - Otherwise, the runtime will think you are attempting * to orphan a record. (1-1) relationship. The user Detail should be removed FIRST. * * To view ALL user information when accessing the UsersAdmin Index, you will need to update it (and the * action) to get the associated user details. */ #region Create User Details Object EVERY TIME a new user Registers //create the userdetail object UserDetail newUser = new UserDetail() { UserID = user.Id, FirstName = model.FirstName, LastName = model.LastName }; //establish a connection to the db BookStorePlusEntities ctx = new BookStorePlusEntities(); //send the object to EF ctx.UserDetails.Add(newUser); //save the changes to the DB ctx.SaveChanges(); #endregion //have them login with their new credentials return(RedirectToAction("Login")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }