Login.cshtml.cs 3.5 KB

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