111 lines
3.4 KiB
Plaintext
111 lines
3.4 KiB
Plaintext
@page "/Account/ChangePassword"
|
|
|
|
@using System.ComponentModel.DataAnnotations
|
|
@using Microsoft.AspNetCore.Identity
|
|
@using CouchLog.Data
|
|
|
|
@inject UserManager<ApplicationUser> UserManager
|
|
@inject SignInManager<ApplicationUser> SignInManager
|
|
@inject IdentityUserAccessor UserAccessor
|
|
@inject IdentityRedirectManager RedirectManager
|
|
@inject ILogger<ChangePassword> Logger
|
|
@inject ISnackbar Snackbar
|
|
|
|
<PageTitle>Change password</PageTitle>
|
|
|
|
<MudText Typo="Typo.h6">Change password</MudText>
|
|
|
|
<EditForm Model="Input" FormName="change-password" OnValidSubmit="OnValidSubmitAsync" method="post">
|
|
|
|
<MudTextField
|
|
T="string"
|
|
name="Input.OldPassword"
|
|
Label="OldPassword"
|
|
@bind-Value="Input.OldPassword"
|
|
For="@(() => Input.OldPassword)"
|
|
InputType="InputType.Password"
|
|
Required="true"
|
|
Variant="Variant.Outlined"
|
|
ShrinkLabel="true"/>
|
|
|
|
<MudTextField
|
|
T="string"
|
|
name="Input.NewPassword"
|
|
Label="New password"
|
|
@bind-Value="Input.NewPassword"
|
|
For="@(() => Input.NewPassword)"
|
|
InputType="InputType.Password"
|
|
Required="true"
|
|
Variant="Variant.Outlined"
|
|
ShrinkLabel="true"/>
|
|
|
|
<MudTextField
|
|
T="string"
|
|
name="Input.ConfirmPassword"
|
|
Label="Confirm password"
|
|
@bind-Value="Input.ConfirmPassword"
|
|
For="@(() => Input.ConfirmPassword)"
|
|
InputType="InputType.Password"
|
|
Required="true"
|
|
Variant="Variant.Outlined"
|
|
ShrinkLabel="true"/>
|
|
|
|
<MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary">Change password</MudButton>
|
|
</EditForm>
|
|
|
|
@code {
|
|
private ApplicationUser _user = null!;
|
|
private bool _hasPassword;
|
|
|
|
[CascadingParameter]
|
|
private HttpContext HttpContext { get; set; } = null!;
|
|
|
|
[SupplyParameterFromForm]
|
|
private InputModel Input { get; set; } = null!;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
_user = await UserAccessor.GetRequiredUserAsync(HttpContext);
|
|
_hasPassword = await UserManager.HasPasswordAsync(_user);
|
|
if (!_hasPassword)
|
|
{
|
|
RedirectManager.RedirectTo("Account/Manage/SetPassword");
|
|
}
|
|
Input ??= new InputModel();
|
|
}
|
|
|
|
private async Task OnValidSubmitAsync()
|
|
{
|
|
var changePasswordResult = await UserManager.ChangePasswordAsync(_user, Input.OldPassword, Input.NewPassword);
|
|
if (!changePasswordResult.Succeeded)
|
|
{
|
|
|
|
return;
|
|
}
|
|
|
|
await SignInManager.RefreshSignInAsync(_user);
|
|
Logger.LogInformation("User changed their password successfully.");
|
|
|
|
RedirectManager.RedirectToCurrentPageWithStatus("Your password has been changed", HttpContext);
|
|
}
|
|
|
|
private sealed class InputModel
|
|
{
|
|
[Required]
|
|
[DataType(DataType.Password)]
|
|
[Display(Name = "Current password")]
|
|
public string OldPassword { get; set; } = "";
|
|
|
|
[Required]
|
|
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
|
|
[DataType(DataType.Password)]
|
|
[Display(Name = "New password")]
|
|
public string NewPassword { get; set; } = "";
|
|
|
|
[DataType(DataType.Password)]
|
|
[Display(Name = "Confirm new password")]
|
|
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
|
|
public string ConfirmPassword { get; set; } = "";
|
|
}
|
|
}
|