Login.cshtml.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. #nullable disable
  4. using Application.Abstractions.Data;
  5. using Domain.Entities.Director;
  6. using Infrastructure.Persistence.Identity;
  7. using Microsoft.AspNetCore.Authentication;
  8. using Microsoft.AspNetCore.Authorization;
  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 System;
  15. using System.Collections.Generic;
  16. using System.ComponentModel.DataAnnotations;
  17. using System.Linq;
  18. using System.Threading.Tasks;
  19. namespace Admin.Areas.Identity.Pages.Account
  20. {
  21. public class LoginModel : PageModel
  22. {
  23. private readonly SignInManager<ApplicationUser> _signInManager;
  24. private readonly ILogger<LoginModel> _logger;
  25. private readonly IAppDbContext _db;
  26. public LoginModel(SignInManager<ApplicationUser> signInManager, ILogger<LoginModel> logger, IAppDbContext db)
  27. {
  28. _signInManager = signInManager;
  29. _logger = logger;
  30. _db = db;
  31. }
  32. /// <summary>
  33. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  34. /// directly from your code. This API may change or be removed in future releases.
  35. /// </summary>
  36. [BindProperty]
  37. public InputModel Input { get; set; }
  38. /// <summary>
  39. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  40. /// directly from your code. This API may change or be removed in future releases.
  41. /// </summary>
  42. public IList<AuthenticationScheme> ExternalLogins { get; set; }
  43. /// <summary>
  44. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  45. /// directly from your code. This API may change or be removed in future releases.
  46. /// </summary>
  47. public string ReturnUrl { get; set; }
  48. /// <summary>
  49. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  50. /// directly from your code. This API may change or be removed in future releases.
  51. /// </summary>
  52. [TempData]
  53. public string ErrorMessage { get; set; }
  54. /// <summary>
  55. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  56. /// directly from your code. This API may change or be removed in future releases.
  57. /// </summary>
  58. public class InputModel
  59. {
  60. /// <summary>
  61. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  62. /// directly from your code. This API may change or be removed in future releases.
  63. /// </summary>
  64. [Required]
  65. [EmailAddress]
  66. public string Email { get; set; }
  67. /// <summary>
  68. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  69. /// directly from your code. This API may change or be removed in future releases.
  70. /// </summary>
  71. [Required]
  72. [DataType(DataType.Password)]
  73. public string Password { get; set; }
  74. /// <summary>
  75. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  76. /// directly from your code. This API may change or be removed in future releases.
  77. /// </summary>
  78. [Display(Name = "로그인 상태 유지")]
  79. public bool RememberMe { get; set; }
  80. }
  81. public async Task OnGetAsync(string returnUrl = null)
  82. {
  83. if (!string.IsNullOrEmpty(ErrorMessage))
  84. {
  85. ModelState.AddModelError(string.Empty, ErrorMessage);
  86. }
  87. returnUrl ??= Url.Content("~/");
  88. // Clear the existing external cookie to ensure a clean login process
  89. await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
  90. ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
  91. ReturnUrl = returnUrl;
  92. }
  93. public async Task<IActionResult> OnPostAsync(string returnUrl = null)
  94. {
  95. returnUrl ??= Url.Content("~/");
  96. ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
  97. if (ModelState.IsValid)
  98. {
  99. var ipAddress = HttpContext.Connection.RemoteIpAddress?.ToString();
  100. var userAgent = HttpContext.Request.Headers.UserAgent.ToString();
  101. if (userAgent?.Length > 512)
  102. {
  103. userAgent = userAgent[..512];
  104. }
  105. // This doesn't count login failures towards account lockout
  106. // To enable password failures to trigger account lockout, set lockoutOnFailure: true
  107. var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);
  108. if (result.Succeeded)
  109. {
  110. await _db.AdminLoginLog.AddAsync(AdminLoginLog.Create(true, Input.Email, ipAddress: ipAddress, userAgent: userAgent));
  111. await _db.SaveChangesAsync();
  112. _logger.LogInformation("User logged in.");
  113. return LocalRedirect(returnUrl);
  114. }
  115. if (result.RequiresTwoFactor)
  116. {
  117. await _db.AdminLoginLog.AddAsync(AdminLoginLog.Create(true, Input.Email, reason: "2FA 필요", ipAddress: ipAddress, userAgent: userAgent));
  118. await _db.SaveChangesAsync();
  119. return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
  120. }
  121. if (result.IsLockedOut)
  122. {
  123. await _db.AdminLoginLog.AddAsync(AdminLoginLog.Create(false, Input.Email, reason: "계정 잠김", ipAddress: ipAddress, userAgent: userAgent));
  124. await _db.SaveChangesAsync();
  125. _logger.LogWarning("User account locked out.");
  126. return RedirectToPage("./Lockout");
  127. }
  128. else
  129. {
  130. await _db.AdminLoginLog.AddAsync(AdminLoginLog.Create(false, Input.Email, reason: "로그인 실패", ipAddress: ipAddress, userAgent: userAgent));
  131. await _db.SaveChangesAsync();
  132. ModelState.AddModelError(string.Empty, "Invalid login attempt.");
  133. return Page();
  134. }
  135. }
  136. // If we got this far, something failed, redisplay form
  137. return Page();
  138. }
  139. }
  140. }