Login.cshtml.cs 5.8 KB

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