@page "/account/change-password"
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
@inject IUserService UserService
@inject IUserContextAccessor UserContext
@inject IAuditService Audit
@rendermode InteractiveServer
@using SharepointToolbox.Web.Core.Models
@using SharepointToolbox.Web.Services.Audit
@using SharepointToolbox.Web.Services.Auth
@using SharepointToolbox.Web.Services.Session
Change Password
@if (!UserContext.IsAuthenticated)
{
You must be signed in.
return;
}
@if (_user is null)
{
Loading…
}
else if (_user.Provider != AuthProvider.Local)
{
Your account signs in with Microsoft (Entra). Manage its password in your Microsoft account.
}
else
{
@if (!string.IsNullOrEmpty(_message))
{
@_message
}
}
@code {
private AppUser? _user;
private string _current = string.Empty;
private string _new = string.Empty;
private string _confirm = string.Empty;
private string _message = string.Empty;
private bool _isError;
protected override async Task OnInitializedAsync()
{
if (UserContext.IsAuthenticated)
_user = await UserService.GetByEmailAsync(UserContext.Email);
}
private async Task SubmitAsync()
{
if (_user is null) return;
if (string.IsNullOrWhiteSpace(_new) || _new != _confirm)
{
_message = "New passwords do not match.";
_isError = true;
return;
}
var ok = await UserService.ChangePasswordAsync(_user.Id, _current, _new);
if (ok)
{
await Audit.LogAsync("PasswordChanged", "", Array.Empty(),
$"Changed own password ({_user.Email}).");
_message = "Password changed.";
_isError = false;
_current = _new = _confirm = string.Empty;
}
else
{
_message = "Current password is incorrect.";
_isError = true;
}
}
}