public async Task<ActionResult> Register(RegisterViewModel model) { setDisplayParams(); if (ModelState.IsValid) { SpicyGardenDbContext data = new SpicyGardenDbContext(); var user = new AccountUser { UserName = model.UserName, Email = model.Email, Validated = false, CreatedDate = DateTime.Now }; var customer = new Customer { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, Telephone = model.Telephone, CreatedDate = DateTime.Now, AccountId = user.Id, Validated = false }; var address = new Address { AddrLine1 = model.AddrLine1, AddrLine2 = model.AddrLine2, CustomerId = customer.Id, AccountId = user.Id, CreatedDate = DateTime.Now, PostalCode = model.PostalCode }; customer.AddressId = address.Id; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { data.Customers.Add(customer); data.Addresses.Add(address); try { data.SaveChanges(); } catch (DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); var fullErrorMessage = string.Join("; ", errorMessages); var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); } await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string 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 <a href=\"" + callbackUrl + "\">here</a>"); // first check html entity encode the return url just in case string return_url = Server.HtmlEncode(Request.Form["r"]); if (return_url == null) { return Redirect("/"); } else { return Redirect(return_url); } } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }
public ActionResult EditAccountInformation(AccountManagementModel model) { setDisplayParams(); setAccountInfo(); SpicyGardenDbContext database = new SpicyGardenDbContext(); if (!ModelState.IsValid) { return View(); } // everything is good so now check if password entered is correct var oldUser = UserManager.Find(User.Identity.Name, model.Password); var cust = this.SpicyGardenDbContext.Customers.Where(c => c.AccountId == oldUser.Id).FirstOrDefault(); var address = this.SpicyGardenDbContext.Addresses.Where(a => a.AccountId == oldUser.Id).FirstOrDefault(); if (oldUser == null) { // wrong password return View(); } oldUser.Email = model.Email; oldUser.ModifiedDate = DateTime.Now; cust.FirstName = model.FirstName; cust.LastName = model.LastName; cust.Telephone = model.Telephone; cust.Email = model.Email; cust.ModifiedDate = DateTime.Now; address.AddrLine1 = model.AddrLine1; address.AddrLine2 = model.AddrLine2; address.PostalCode = model.PostalCode; address.ModifiedDate = DateTime.Now; try { var result = UserManager.Update(oldUser); if (result.Succeeded) { database.Customers.Attach(cust); database.Entry(cust).State = EntityState.Modified; database.Addresses.Attach(address); database.Entry(address).State = EntityState.Modified; database.SaveChanges(); return Redirect("/Account/Manage"); } } catch (DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); var fullErrorMessage = string.Join("; ", errorMessages); var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); } return View(); }