using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; namespace MiniMarket.Web.Areas.Identity.Pages.Account { [AllowAnonymous] public class LoginModel : PageModel { private readonly SignInManager _signInManager; public LoginModel(SignInManager signInManager) { _signInManager = signInManager; } [BindProperty] public InputModel Input { get; set; } public string ReturnUrl { get; set; } [TempData] public string ErrorMessage { get; set; } public class InputModel { [Required] [EmailAddress] public string Email { get; set; } [Required] [DataType(DataType.Password)] public string Password { get; set; } } public async Task OnGetAsync(string returnUrl = null) { if (!string.IsNullOrEmpty(ErrorMessage)) { ModelState.AddModelError(string.Empty, ErrorMessage); } returnUrl ??= Url.Content("~/"); // Limpiar cookie externa para asegurar un login limpio await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme); ReturnUrl = returnUrl; } public async Task OnPostAsync(string returnUrl = null) { returnUrl ??= Url.Content("~/"); if (ModelState.IsValid) { var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, false, lockoutOnFailure: false); if (result.Succeeded) { return LocalRedirect(returnUrl); } else { ModelState.AddModelError(string.Empty, "Intento de inicio de sesión no válido."); return Page(); } } return Page(); } } }