public async Task<ActionResult> Edit() { var UserId = User.Identity.GetUserId(); AppUser user = await UserManager.FindByIdAsync(UserId); if (user != null) { UserEditModel editUser = new UserEditModel { ID = UserId, FirstName = user.FirstName, LastName = user.LastName, UserName = user.UserName, Email = user.Email, Address = user.Address, Zip = user.Zip, Country = user.Country, Avatar = user.Avatar, Biography = user.Biography, Birthday = user.Birthday, }; if (user.City != null) { editUser.City = (Cities)System.Enum.Parse(typeof(Cities), user.City); } if (user.Gender != null) { editUser.Gender = (Gender)System.Enum.Parse(typeof(Gender), user.Gender); } if (user.City != null) { editUser.PrimaryLanguage = (Language)System.Enum.Parse(typeof(Language), user.PrimaryLanguage); } return View(editUser); } else { return RedirectToAction("Index"); } }
public async Task<ActionResult> Edit(UserEditModel model, string password) { AppUser user = await UserManager.FindByIdAsync(model.ID); if (user != null) { user.FirstName = model.FirstName; user.LastName = model.LastName; user.Address = model.Address; user.Zip = model.Zip; user.City = model.City.ToString(); user.Country = "Danmark"; user.Avatar = model.Avatar; user.Gender = model.Gender.ToString(); user.Biography = model.Biography; user.Birthday = model.Birthday; user.PrimaryLanguage = model.PrimaryLanguage.ToString(); IdentityResult validEmail = await UserManager.UserValidator.ValidateAsync(user); bool validPassword = await UserManager.CheckPasswordAsync(user, password); if (!validEmail.Succeeded) { AddErrorsFromResult(validEmail); } //IdentityResult validPass = null; //if (password != string.Empty) //{ // validPass = await UserManager.PasswordValidator.ValidateAsync(password); // if (validPass.Succeeded) // { // user.PasswordHash = UserManager.PasswordHasher.HashPassword(password); // } // else // { // AddErrorsFromResult(validPass); // } //} //if ((validEmail.Succeeded && validPass == null) || (validEmail.Succeeded && password != string.Empty && validPass.Succeeded)) if (validEmail.Succeeded && validPassword) { IdentityResult result = await UserManager.UpdateAsync(user); if (result.Succeeded) { return RedirectToAction("Index"); } else { AddErrorsFromResult(result); } } else { ModelState.AddModelError("", "Please Confirm that the password is correct."); } } return View(model); }
public async Task<ActionResult> Edit(string id) { AppUser user = await UserManager.FindByIdAsync(id); if (user != null) { UserEditModel editUser = new UserEditModel { ID = id, FirstName = user.FirstName, LastName = user.LastName, UserName = user.UserName, Email = user.Email, Address = user.Address, Zip = user.Zip, Country = user.Country, Avatar = user.Avatar, Biography = user.Biography, Birthday = user.Birthday, }; //Possibility to add users to roles in User view //editUser.Roles = new List<string>(); //foreach (IdentityUserRole role in user.Roles) //{ // editUser.Roles.Add(role.RoleId); //} if (user.City != null) { editUser.City = (Cities)System.Enum.Parse(typeof(Cities), user.City); } if (user.Gender != null) { editUser.Gender = (Gender)System.Enum.Parse(typeof(Gender), user.Gender); } if (user.City != null) { editUser.PrimaryLanguage = (Language)System.Enum.Parse(typeof(Language), user.PrimaryLanguage); } //ViewBag.Roles = new MultiSelectList(db.Roles, "Id", "Name"); return View(editUser); } else { return RedirectToAction("Index"); } }