Login.cshtml.cs 3.7 KB

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