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 Infrastructure.Persistence.Identity;
  5. using Microsoft.AspNetCore.Authentication;
  6. using Microsoft.AspNetCore.Authorization;
  7. using Microsoft.AspNetCore.Identity;
  8. using Microsoft.AspNetCore.Identity.UI.Services;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Microsoft.AspNetCore.Mvc.RazorPages;
  11. using Microsoft.Extensions.Logging;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.ComponentModel.DataAnnotations;
  15. using System.Linq;
  16. using System.Threading.Tasks;
  17. namespace Admin.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 = "·Î±×ÀÎ »óÅ À¯Áö")]
  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. }