Login.cshtml.cs 5.7 KB

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