Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Login.cshtml.cs 3.1KB

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