ResetPassword.razor 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. @page "/Account/ResetPassword"
  2. @using System.ComponentModel.DataAnnotations
  3. @using System.Text
  4. @using Microsoft.AspNetCore.Identity
  5. @using Microsoft.AspNetCore.WebUtilities
  6. @using ConcursoProjetos.Data
  7. @inject IdentityRedirectManager RedirectManager
  8. @inject UserManager<ApplicationUser> UserManager
  9. <PageTitle>Troca de Senha</PageTitle>
  10. <h2>Troca de Senha</h2>
  11. <hr />
  12. <div class="row">
  13. <div class="col-md-4">
  14. <StatusMessage Message="@Message" />
  15. <EditForm Model="Input" FormName="reset-password" OnValidSubmit="OnValidSubmitAsync" method="post">
  16. <DataAnnotationsValidator />
  17. <ValidationSummary class="text-danger" role="alert" />
  18. <input type="hidden" name="Input.Code" value="@Input.Code" />
  19. <div class="form-floating mb-3">
  20. <InputText @bind-Value="Input.Email" class="form-control" autocomplete="username" aria-required="true" placeholder="name@example.com" />
  21. <label for="email" class="form-label">E-mail</label>
  22. <ValidationMessage For="() => Input.Email" class="text-danger" />
  23. </div>
  24. <div class="form-floating mb-3">
  25. <InputText type="password" @bind-Value="Input.Password" class="form-control" autocomplete="new-password" aria-required="true" placeholder="Por favor, entre com sua senha." />
  26. <label for="password" class="form-label">Senha</label>
  27. <ValidationMessage For="() => Input.Password" class="text-danger" />
  28. </div>
  29. <div class="form-floating mb-3">
  30. <InputText type="password" @bind-Value="Input.ConfirmPassword" class="form-control" autocomplete="new-password" aria-required="true" placeholder="Por favor, confirme seu nova senha." />
  31. <label for="confirm-password" class="form-label">Confirm password</label>
  32. <ValidationMessage For="() => Input.ConfirmPassword" class="text-danger" />
  33. </div>
  34. <button type="submit" class="w-100 btn btn-lg btn-primary">Trocar</button>
  35. </EditForm>
  36. </div>
  37. </div>
  38. @code {
  39. private IEnumerable<IdentityError>? identityErrors;
  40. [SupplyParameterFromForm]
  41. private InputModel Input { get; set; } = new();
  42. [SupplyParameterFromQuery]
  43. private string? Code { get; set; }
  44. private string? Message => identityErrors is null ? null : $"Error: {string.Join(", ", identityErrors.Select(error => error.Description))}";
  45. protected override void OnInitialized()
  46. {
  47. if (Code is null)
  48. {
  49. RedirectManager.RedirectTo("Account/InvalidPasswordReset");
  50. }
  51. Input.Code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(Code));
  52. }
  53. private async Task OnValidSubmitAsync()
  54. {
  55. var user = await UserManager.FindByEmailAsync(Input.Email);
  56. if (user is null)
  57. {
  58. // Don't reveal that the user does not exist
  59. RedirectManager.RedirectTo("Account/ResetPasswordConfirmation");
  60. }
  61. var result = await UserManager.ResetPasswordAsync(user, Input.Code, Input.Password);
  62. if (result.Succeeded)
  63. {
  64. RedirectManager.RedirectTo("Account/ResetPasswordConfirmation");
  65. }
  66. identityErrors = result.Errors;
  67. }
  68. private sealed class InputModel
  69. {
  70. [Required]
  71. [EmailAddress]
  72. public string Email { get; set; } = "";
  73. [Required]
  74. [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
  75. [DataType(DataType.Password)]
  76. public string Password { get; set; } = "";
  77. [DataType(DataType.Password)]
  78. [Display(Name = "Confirm password")]
  79. [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
  80. public string ConfirmPassword { get; set; } = "";
  81. [Required]
  82. public string Code { get; set; } = "";
  83. }
  84. }