| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.ComponentModel.DataAnnotations; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Text.Encodings.Web; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using Microsoft.AspNetCore.Authorization; |
| | | 8 | | using Microsoft.AspNetCore.Authentication; |
| | | 9 | | using Microsoft.AspNetCore.Identity; |
| | | 10 | | using Microsoft.AspNetCore.Identity.UI.Services; |
| | | 11 | | using Microsoft.AspNetCore.Mvc; |
| | | 12 | | using Microsoft.AspNetCore.Mvc.RazorPages; |
| | | 13 | | using Microsoft.Extensions.Logging; |
| | | 14 | | using MUNity.Database.Models.User; |
| | | 15 | | |
| | | 16 | | namespace MUNityCore.Areas.Identity.Pages.Account |
| | | 17 | | { |
| | | 18 | | [AllowAnonymous] |
| | | 19 | | public class LoginModel : PageModel |
| | | 20 | | { |
| | | 21 | | private readonly UserManager<MunityUser> _userManager; |
| | | 22 | | private readonly SignInManager<MunityUser> _signInManager; |
| | | 23 | | private readonly ILogger<LoginModel> _logger; |
| | | 24 | | |
| | 0 | 25 | | public LoginModel(SignInManager<MunityUser> signInManager, |
| | 0 | 26 | | ILogger<LoginModel> logger, |
| | 0 | 27 | | UserManager<MunityUser> userManager) |
| | 0 | 28 | | { |
| | 0 | 29 | | _userManager = userManager; |
| | 0 | 30 | | _signInManager = signInManager; |
| | 0 | 31 | | _logger = logger; |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | [BindProperty] |
| | 0 | 35 | | public InputModel Input { get; set; } |
| | | 36 | | |
| | | 37 | | [BindProperty] |
| | 0 | 38 | | public OutputModel LoginOutput { get; set; } |
| | | 39 | | |
| | 0 | 40 | | public IList<AuthenticationScheme> ExternalLogins { get; set; } |
| | | 41 | | |
| | 0 | 42 | | public string ReturnUrl { get; set; } |
| | | 43 | | |
| | | 44 | | [TempData] |
| | 0 | 45 | | public string ErrorMessage { get; set; } |
| | | 46 | | |
| | | 47 | | public class InputModel |
| | | 48 | | { |
| | | 49 | | [Required] |
| | 0 | 50 | | public string Username { get; set; } |
| | | 51 | | |
| | | 52 | | [Required] |
| | | 53 | | [DataType(DataType.Password)] |
| | 0 | 54 | | public string Password { get; set; } |
| | | 55 | | |
| | | 56 | | [Display(Name = "Remember me?")] |
| | 0 | 57 | | public bool RememberMe { get; set; } |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | public class OutputModel |
| | | 61 | | { |
| | 0 | 62 | | public bool InvalidInput { get; set; } |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public async Task OnGetAsync(string returnUrl = null) |
| | 0 | 66 | | { |
| | 0 | 67 | | if (!string.IsNullOrEmpty(ErrorMessage)) |
| | 0 | 68 | | { |
| | 0 | 69 | | ModelState.AddModelError(string.Empty, ErrorMessage); |
| | 0 | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | returnUrl ??= Url.Content("~/"); |
| | | 73 | | |
| | | 74 | | // Clear the existing external cookie to ensure a clean login process |
| | 0 | 75 | | await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme); |
| | | 76 | | |
| | 0 | 77 | | ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); |
| | | 78 | | |
| | 0 | 79 | | ReturnUrl = returnUrl; |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | public async Task<IActionResult> OnPostAsync(string returnUrl = null) |
| | 0 | 83 | | { |
| | 0 | 84 | | returnUrl ??= Url.Content("~/"); |
| | | 85 | | |
| | 0 | 86 | | ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); |
| | | 87 | | |
| | 0 | 88 | | if (ModelState.IsValid) |
| | 0 | 89 | | { |
| | | 90 | | // This doesn't count login failures towards account lockout |
| | | 91 | | // To enable password failures to trigger account lockout, set lockoutOnFailure: true |
| | 0 | 92 | | var result = await _signInManager.PasswordSignInAsync(Input.Username, Input.Password, Input.RememberMe, |
| | 0 | 93 | | if (result.Succeeded) |
| | 0 | 94 | | { |
| | 0 | 95 | | _logger.LogInformation($"{Input.Username} logged in."); |
| | 0 | 96 | | return LocalRedirect(returnUrl); |
| | | 97 | | } |
| | 0 | 98 | | if (result.RequiresTwoFactor) |
| | 0 | 99 | | { |
| | 0 | 100 | | return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe } |
| | | 101 | | } |
| | 0 | 102 | | if (result.IsLockedOut) |
| | 0 | 103 | | { |
| | 0 | 104 | | _logger.LogWarning("User account locked out."); |
| | 0 | 105 | | return RedirectToPage("./Logout"); |
| | | 106 | | } |
| | | 107 | | else |
| | 0 | 108 | | { |
| | 0 | 109 | | ModelState.AddModelError(string.Empty, "Invalid login attempt."); |
| | 0 | 110 | | return Page(); |
| | | 111 | | } |
| | | 112 | | } |
| | | 113 | | else |
| | 0 | 114 | | { |
| | 0 | 115 | | LoginOutput.InvalidInput = true; |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | // If we got this far, something failed, redisplay form |
| | 0 | 119 | | return Page(); |
| | 0 | 120 | | } |
| | | 121 | | } |
| | | 122 | | } |