123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- @page "/Account/ResetPassword"
- @using System.ComponentModel.DataAnnotations
- @using System.Text
- @using Microsoft.AspNetCore.Identity
- @using Microsoft.AspNetCore.WebUtilities
- @using ConcursoProjetos.Data
- @inject IdentityRedirectManager RedirectManager
- @inject UserManager<ApplicationUser> UserManager
- <PageTitle>Troca de Senha</PageTitle>
- <h2>Troca de Senha</h2>
- <hr />
- <div class="row">
- <div class="col-md-4">
- <StatusMessage Message="@Message" />
- <EditForm Model="Input" FormName="reset-password" OnValidSubmit="OnValidSubmitAsync" method="post">
- <DataAnnotationsValidator />
- <ValidationSummary class="text-danger" role="alert" />
- <input type="hidden" name="Input.Code" value="@Input.Code" />
- <div class="form-floating mb-3">
- <InputText @bind-Value="Input.Email" class="form-control" autocomplete="username" aria-required="true" placeholder="name@example.com" />
- <label for="email" class="form-label">E-mail</label>
- <ValidationMessage For="() => Input.Email" class="text-danger" />
- </div>
- <div class="form-floating mb-3">
- <InputText type="password" @bind-Value="Input.Password" class="form-control" autocomplete="new-password" aria-required="true" placeholder="Por favor, entre com sua senha." />
- <label for="password" class="form-label">Senha</label>
- <ValidationMessage For="() => Input.Password" class="text-danger" />
- </div>
- <div class="form-floating mb-3">
- <InputText type="password" @bind-Value="Input.ConfirmPassword" class="form-control" autocomplete="new-password" aria-required="true" placeholder="Por favor, confirme seu nova senha." />
- <label for="confirm-password" class="form-label">Confirm password</label>
- <ValidationMessage For="() => Input.ConfirmPassword" class="text-danger" />
- </div>
- <button type="submit" class="w-100 btn btn-lg btn-primary">Trocar</button>
- </EditForm>
- </div>
- </div>
- @code {
- private IEnumerable<IdentityError>? identityErrors;
- [SupplyParameterFromForm]
- private InputModel Input { get; set; } = new();
- [SupplyParameterFromQuery]
- private string? Code { get; set; }
- private string? Message => identityErrors is null ? null : $"Error: {string.Join(", ", identityErrors.Select(error => error.Description))}";
- protected override void OnInitialized()
- {
- if (Code is null)
- {
- RedirectManager.RedirectTo("Account/InvalidPasswordReset");
- }
- Input.Code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(Code));
- }
- private async Task OnValidSubmitAsync()
- {
- var user = await UserManager.FindByEmailAsync(Input.Email);
- if (user is null)
- {
- // Don't reveal that the user does not exist
- RedirectManager.RedirectTo("Account/ResetPasswordConfirmation");
- }
- var result = await UserManager.ResetPasswordAsync(user, Input.Code, Input.Password);
- if (result.Succeeded)
- {
- RedirectManager.RedirectTo("Account/ResetPasswordConfirmation");
- }
- identityErrors = result.Errors;
- }
- private sealed class InputModel
- {
- [Required]
- [EmailAddress]
- public string Email { get; set; } = "";
- [Required]
- [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
- [DataType(DataType.Password)]
- public string Password { get; set; } = "";
- [DataType(DataType.Password)]
- [Display(Name = "Confirm password")]
- [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
- public string ConfirmPassword { get; set; } = "";
- [Required]
- public string Code { get; set; } = "";
- }
- }
|