Advertisement
Guest User

Untitled

a guest
Aug 5th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. [HttpPost]
  2. public async Task<ActionResult> Edit(string id, string email, string password)
  3. {
  4. AppUser user = await this.UserManager.FindByIdAsync(id);
  5. if (user != null)
  6. {
  7. user.Email = email;
  8. IdentityResult validEmail = await this.UserManager.UserValidator.ValidateAsync(user);
  9. if (!validEmail.Succeeded)
  10. {
  11. this.AddErrorsFromResult(validEmail);
  12. }
  13.  
  14. IdentityResult validPass = null;
  15. if (password != string.Empty)
  16. {
  17. validPass = await this.UserManager.PasswordValidator.ValidateAsync(password);
  18. if (validPass.Succeeded)
  19. {
  20. user.PasswordHash = this.UserManager.PasswordHasher.HashPassword(password);
  21. }
  22. else
  23. {
  24. this.AddErrorsFromResult(validPass);
  25. }
  26. }
  27.  
  28. if ((validEmail.Succeeded && validPass == null)
  29. || (validEmail.Succeeded && password != string.Empty && validPass.Succeeded))
  30. {
  31. IdentityResult result = await this.UserManager.UpdateAsync(user);
  32. if (result.Succeeded)
  33. {
  34. return this.RedirectToAction("Index");
  35. }
  36.  
  37. this.AddErrorsFromResult(result);
  38. }
  39. }
  40. else
  41. {
  42. ModelState.AddModelError(string.Empty, "User not found");
  43. }
  44.  
  45. return this.View(user);
  46. }
  47.  
  48. private AppUserManager UserManager
  49. {
  50. get
  51. {
  52. return HttpContext.GetOwinContext().GetUserManager<AppUserManager>();
  53. }
  54. }
  55. private void AddErrorsFromResult(IdentityResult result)
  56. {
  57. foreach (string error in result.Errors)
  58. {
  59. ModelState.AddModelError(string.Empty, error);
  60. }
  61. }
  62.  
  63. public virtual async Task<IdentityResult> UpdateAsync(TUser user)
  64. {
  65. ThrowIfDisposed();
  66. if (user == null)
  67. {
  68. throw new ArgumentNullException("user");
  69. }
  70.  
  71. var result = await UserValidator.ValidateAsync(user).ConfigureAwait(false);
  72. if (!result.Succeeded)
  73. {
  74. return result;
  75. }
  76. await Store.UpdateAsync(user).ConfigureAwait(false);
  77. return IdentityResult.Success;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement