79 lines
2.5 KiB
Text
79 lines
2.5 KiB
Text
@page "/Profile/ChangePassword"
|
|
@using Microsoft.AspNetCore.Components.Authorization;
|
|
@inject UserService UserService
|
|
@inject IMessageService MessageService
|
|
@inject AuthenticationStateProvider AuthenticationStateProvider
|
|
@inject ILogger<ChangePassword> Logger
|
|
@attribute [Authorize]
|
|
|
|
<PageHeader Title="Change Password" />
|
|
|
|
<div style="padding: 0 24px;">
|
|
<Form Model="Model" Layout="FormLayout.Vertical">
|
|
@if (!String.IsNullOrWhiteSpace(User.PasswordHash))
|
|
{
|
|
<FormItem Label="Current Password">
|
|
<InputPassword @bind-Value="context.CurrentPassword"/>
|
|
</FormItem>
|
|
}
|
|
|
|
<FormItem Label="New Password">
|
|
<InputPassword @bind-Value="context.NewPassword"/>
|
|
</FormItem>
|
|
|
|
<FormItem Label="Confirm Password">
|
|
<InputPassword @bind-Value="context.NewPasswordConfirm"/>
|
|
</FormItem>
|
|
|
|
<FormItem>
|
|
<Button OnClick="Change" Type="@ButtonType.Primary">Change</Button>
|
|
</FormItem>
|
|
</Form>
|
|
</div>
|
|
|
|
@code {
|
|
User User = new User();
|
|
ChangePasswordModel Model = new ChangePasswordModel();
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
|
|
|
|
if (authState.User.Identity.IsAuthenticated)
|
|
User = await UserService.GetAsync(authState.User.Identity.Name);
|
|
}
|
|
|
|
async void Change()
|
|
{
|
|
try
|
|
{
|
|
if (!String.IsNullOrWhiteSpace(User.PasswordHash) && !(await UserService.CheckPassword(User.UserName, Model.CurrentPassword)))
|
|
{
|
|
MessageService.Error("Incorrect password!");
|
|
return;
|
|
}
|
|
|
|
if (Model.NewPassword == Model.NewPasswordConfirm)
|
|
{
|
|
var result = await UserService.ChangePassword(User.UserName, Model.CurrentPassword, Model.NewPassword);
|
|
|
|
if (result.Succeeded)
|
|
MessageService.Success("Password changed!");
|
|
}
|
|
else
|
|
MessageService.Error("Passwords don't match!");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await MessageService.Error("Password could not be changed!");
|
|
Logger.LogError(ex, "Password could not be changed!");
|
|
}
|
|
}
|
|
|
|
public class ChangePasswordModel
|
|
{
|
|
public string CurrentPassword { get; set; }
|
|
public string NewPassword { get; set; }
|
|
public string NewPasswordConfirm { get; set; }
|
|
}
|
|
}
|