@page "/Account/Login" @using System.ComponentModel.DataAnnotations @using Microsoft.AspNetCore.Authentication @using Microsoft.AspNetCore.Identity @using ApplianceRepair @inject SignInManager SignInManager @inject ILogger Logger @inject NavigationManager NavigationManager @inject IdentityRedirectManager RedirectManager @inject BusinessConfigReader ConfigReader Login
@code { private string? errorMessage; [CascadingParameter] private HttpContext HttpContext { get; set; } = default!; [SupplyParameterFromForm] private InputModel Input { get; set; } = default!; [SupplyParameterFromQuery] private string? ReturnUrl { get; set; } protected override async Task OnInitializedAsync() { Input ??= new(); if (HttpMethods.IsGet(HttpContext.Request.Method)) { await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme); } } public async Task LoginUser() { var result = await SignInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false); if (result.Succeeded) { Logger.LogInformation("Admin user logged in."); RedirectManager.RedirectTo(ReturnUrl); } else if (result.IsLockedOut) { Logger.LogWarning("Account locked."); RedirectManager.RedirectTo("Account/Lockout"); } else { errorMessage = "Invalid login credentials."; } } private sealed class InputModel { [Required] [EmailAddress] public string Email { get; set; } = ""; [Required] [DataType(DataType.Password)] public string Password { get; set; } = ""; public bool RememberMe { get; set; } } }