You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Login.cshtml.cs 3.0KB

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