public async Task <IActionResult> ChangeProfilePhoto() { var user = await userManager.GetUserAsync(User); if (user == null) { throw new ApplicationException($"Unable to load user with ID '{userManager.GetUserId(User)}'."); } var model = new ChangeProfilePhotoModel { StatusMessage = StatusMessage }; return(View(model)); }
public async Task <IActionResult> ChangeProfilePhoto(ChangeProfilePhotoModel model) { if (!ModelState.IsValid) { return(View(model)); } var user = await userManager.GetUserAsync(User); if (user == null) { throw new ApplicationException($"Unable to load user with ID '{userManager.GetUserId(User)}'."); } // If image exists, then convert and store into byte array if (Request.Form.Files.Count > 0) { byte[] profileData = null; var file = Request.Form.Files["Profile Photo"]; // Convert image to binary array using (var ms = new MemoryStream()) { file.CopyTo(ms); profileData = ms.ToArray(); } // Update profile photo user.ProfilePhoto = profileData; var changeProfilePhotoResult = await userManager.UpdateAsync(user); if (!changeProfilePhotoResult.Succeeded) { AddErrors(changeProfilePhotoResult); return(View(model)); } } await signInManager.SignInAsync(user, isPersistent : false); StatusMessage = "Your profile photo has been changed."; return(RedirectToAction(nameof(ChangeProfilePhoto))); }