public IActionResult ChangeDetails() { if (_signInManager.IsSignedIn(User)) { PizzaUser UserData = new UserHelper(_userManager.GetUserId(HttpContext.User)).user; ChangeDetailsModel model = new ChangeDetailsModel { EmailAddress = UserData.Email, ReceivesEmails = UserData.ReceivesEmails, FirstName = UserData.FirstName, LastName = UserData.LastName, PhoneNumber = UserData.PhoneNumber, PhoneExtension = UserData.PhoneExtension, /*Country = UserData.Country,*/ StreetAddress = UserData.StreetAddress, ApartmentNumber = UserData.ApartmentNumber, City = UserData.City, State = UserData.State, ZipCode = UserData.ZipCode }; return(View(model)); } else { return(RedirectToAction("Login", "Pizza")); } }
public async Task <IActionResult> ChangeDetails(ChangeDetailsModel model) { if (ModelState.IsValid) { Task <PizzaUser> userLookup = _userManager.FindByIdAsync(_userManager.GetUserId(HttpContext.User)); userLookup.Wait(); if (userLookup.IsCompletedSuccessfully) { if (_userManager.PasswordHasher.VerifyHashedPassword( userLookup.Result, userLookup.Result.PasswordHash, model.CurrentPasswordHash ).ToString() == "Success") { UserHelper helper = new UserHelper(_userManager.GetUserId(HttpContext.User)); string lowercaseEmail = model.EmailAddress.ToLower(); string uppercaseEmail = model.EmailAddress.ToUpper(); if (helper.user.NormalizedEmail == uppercaseEmail || (await helper.manager.FindByEmailAsync(lowercaseEmail)) == null) { helper.user.Email = lowercaseEmail; helper.user.NormalizedEmail = uppercaseEmail; helper.user.UserName = lowercaseEmail; helper.user.NormalizedUserName = uppercaseEmail; helper.user.ReceivesEmails = model.ReceivesEmails; helper.user.FirstName = model.FirstName; helper.user.LastName = model.LastName; helper.user.PhoneNumber = NormalizePhoneNumber(model.PhoneNumber); helper.user.PhoneExtension = model.PhoneExtension; helper.user.Country = model.Country; helper.user.StreetAddress = model.StreetAddress; helper.user.ApartmentNumber = model.ApartmentNumber; helper.user.City = model.City; helper.user.State = model.State; helper.user.ZipCode = model.ZipCode; var firstResult = await helper.manager.UpdateAsync(helper.user); if (firstResult.Succeeded) { bool secondResult = true; try { await helper.db.SaveChangesAsync(); } catch { ModelState.AddModelError("", "Sorry, our database failed to update. Please try again in a few minutes."); secondResult = false; } if (secondResult) { return(RedirectToAction("Account", "Pizza")); } } else { AddErrors(firstResult); } } else { ModelState.AddModelError("", "The email address you entered is already registered to another account."); } } else { ModelState.AddModelError("", "The password you entered is incorrect."); } } else { ModelState.AddModelError("", userLookup.Exception.Message); } } return(View(model)); }