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