mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 19:24:27 +00:00
91 lines
3.4 KiB
Plaintext
91 lines
3.4 KiB
Plaintext
@page "/Account/Manage/SetPassword"
|
|
|
|
@using System.ComponentModel.DataAnnotations
|
|
@using Microsoft.AspNetCore.Identity
|
|
@using Aaru.Server.New.Data
|
|
|
|
@inject UserManager<ApplicationUser> UserManager
|
|
@inject SignInManager<ApplicationUser> SignInManager
|
|
@inject IdentityUserAccessor UserAccessor
|
|
@inject IdentityRedirectManager RedirectManager
|
|
|
|
<PageTitle>Set password</PageTitle>
|
|
|
|
<h3>Set your password</h3>
|
|
<StatusMessage Message="@message"/>
|
|
<p class="text-info">
|
|
You do not have a local username/password for this site. Add a local
|
|
account so you can log in without an external login.
|
|
</p>
|
|
<div class="row">
|
|
<div class="col-md-6">
|
|
<EditForm FormName="set-password" method="post" Model="Input" OnValidSubmit="OnValidSubmitAsync">
|
|
<DataAnnotationsValidator/>
|
|
<ValidationSummary class="text-danger" role="alert"/>
|
|
<div class="form-floating mb-3">
|
|
<InputText autocomplete="new-password" @bind-Value="Input.NewPassword" class="form-control" placeholder="Please enter your new password." type="password"/>
|
|
<label class="form-label" for="new-password">New password</label>
|
|
<ValidationMessage class="text-danger" For="() => Input.NewPassword"/>
|
|
</div>
|
|
<div class="form-floating mb-3">
|
|
<InputText autocomplete="new-password" @bind-Value="Input.ConfirmPassword" class="form-control" placeholder="Please confirm your new password." type="password"/>
|
|
<label class="form-label" for="confirm-password">Confirm password</label>
|
|
<ValidationMessage class="text-danger" For="() => Input.ConfirmPassword"/>
|
|
</div>
|
|
<button class="btn btn-lg btn-primary w-100" type="submit">Set password</button>
|
|
</EditForm>
|
|
</div>
|
|
</div>
|
|
|
|
@code {
|
|
private string? message;
|
|
private ApplicationUser user = default!;
|
|
|
|
[CascadingParameter]
|
|
private HttpContext HttpContext { get; set; } = default!;
|
|
|
|
[SupplyParameterFromForm]
|
|
private InputModel Input { get; set; } = new();
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
user = await UserAccessor.GetRequiredUserAsync(HttpContext);
|
|
|
|
var hasPassword = await UserManager.HasPasswordAsync(user);
|
|
|
|
if(hasPassword)
|
|
{
|
|
RedirectManager.RedirectTo("Account/Manage/ChangePassword");
|
|
}
|
|
}
|
|
|
|
private async Task OnValidSubmitAsync()
|
|
{
|
|
var addPasswordResult = await UserManager.AddPasswordAsync(user, Input.NewPassword!);
|
|
|
|
if(!addPasswordResult.Succeeded)
|
|
{
|
|
message = $"Error: {string.Join(",", addPasswordResult.Errors.Select(error => error.Description))}";
|
|
|
|
return;
|
|
}
|
|
|
|
await SignInManager.RefreshSignInAsync(user);
|
|
RedirectManager.RedirectToCurrentPageWithStatus("Your password has been set.", HttpContext);
|
|
}
|
|
|
|
private sealed class InputModel
|
|
{
|
|
[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; }
|
|
}
|
|
|
|
} |