[2aea0fd] | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
---|
| 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
---|
| 3 | #nullable disable
|
---|
| 4 |
|
---|
| 5 | using System;
|
---|
| 6 | using System.Collections.Generic;
|
---|
| 7 | using System.ComponentModel.DataAnnotations;
|
---|
| 8 | using System.Linq;
|
---|
| 9 | using System.Threading.Tasks;
|
---|
| 10 | using Microsoft.AspNetCore.Authorization;
|
---|
| 11 | using Microsoft.AspNetCore.Authentication;
|
---|
| 12 | using Microsoft.AspNetCore.Identity;
|
---|
| 13 | using Microsoft.AspNetCore.Identity.UI.Services;
|
---|
| 14 | using Microsoft.AspNetCore.Mvc;
|
---|
| 15 | using Microsoft.AspNetCore.Mvc.RazorPages;
|
---|
| 16 | using Microsoft.Extensions.Logging;
|
---|
| 17 |
|
---|
| 18 | namespace PostgreSqlDotnetCore.Areas.Identity.Pages.Account
|
---|
| 19 | {
|
---|
| 20 | public class LoginModel : PageModel
|
---|
| 21 | {
|
---|
| 22 | private readonly SignInManager<IdentityUser> _signInManager;
|
---|
| 23 | private readonly ILogger<LoginModel> _logger;
|
---|
| 24 |
|
---|
| 25 | public LoginModel(SignInManager<IdentityUser> signInManager, ILogger<LoginModel> logger)
|
---|
| 26 | {
|
---|
| 27 | _signInManager = signInManager;
|
---|
| 28 | _logger = logger;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
| 33 | /// directly from your code. This API may change or be removed in future releases.
|
---|
| 34 | /// </summary>
|
---|
| 35 | [BindProperty]
|
---|
| 36 | public InputModel Input { get; set; }
|
---|
| 37 |
|
---|
| 38 | /// <summary>
|
---|
| 39 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
| 40 | /// directly from your code. This API may change or be removed in future releases.
|
---|
| 41 | /// </summary>
|
---|
| 42 | public IList<AuthenticationScheme> ExternalLogins { get; set; }
|
---|
| 43 |
|
---|
| 44 | /// <summary>
|
---|
| 45 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
| 46 | /// directly from your code. This API may change or be removed in future releases.
|
---|
| 47 | /// </summary>
|
---|
| 48 | public string ReturnUrl { get; set; }
|
---|
| 49 |
|
---|
| 50 | /// <summary>
|
---|
| 51 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
| 52 | /// directly from your code. This API may change or be removed in future releases.
|
---|
| 53 | /// </summary>
|
---|
| 54 | [TempData]
|
---|
| 55 | public string ErrorMessage { get; set; }
|
---|
| 56 |
|
---|
| 57 | /// <summary>
|
---|
| 58 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
| 59 | /// directly from your code. This API may change or be removed in future releases.
|
---|
| 60 | /// </summary>
|
---|
| 61 | public class InputModel
|
---|
| 62 | {
|
---|
| 63 | /// <summary>
|
---|
| 64 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
| 65 | /// directly from your code. This API may change or be removed in future releases.
|
---|
| 66 | /// </summary>
|
---|
| 67 | [Required]
|
---|
| 68 | [EmailAddress]
|
---|
| 69 | public string Email { get; set; }
|
---|
| 70 |
|
---|
| 71 | /// <summary>
|
---|
| 72 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
| 73 | /// directly from your code. This API may change or be removed in future releases.
|
---|
| 74 | /// </summary>
|
---|
| 75 | [Required]
|
---|
| 76 | [DataType(DataType.Password)]
|
---|
| 77 | public string Password { get; set; }
|
---|
| 78 |
|
---|
| 79 | /// <summary>
|
---|
| 80 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
| 81 | /// directly from your code. This API may change or be removed in future releases.
|
---|
| 82 | /// </summary>
|
---|
| 83 | [Display(Name = "Remember me?")]
|
---|
| 84 | public bool RememberMe { get; set; }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | public async Task OnGetAsync(string returnUrl = null)
|
---|
| 88 | {
|
---|
| 89 | if (!string.IsNullOrEmpty(ErrorMessage))
|
---|
| 90 | {
|
---|
| 91 | ModelState.AddModelError(string.Empty, ErrorMessage);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | returnUrl ??= Url.Content("~/");
|
---|
| 95 |
|
---|
| 96 | // Clear the existing external cookie to ensure a clean login process
|
---|
| 97 | await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
|
---|
| 98 |
|
---|
| 99 | ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
|
---|
| 100 |
|
---|
| 101 | ReturnUrl = returnUrl;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | public async Task<IActionResult> OnPostAsync(string returnUrl = null)
|
---|
| 105 | {
|
---|
| 106 | returnUrl ??= Url.Content("~/");
|
---|
| 107 |
|
---|
| 108 | ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
|
---|
| 109 |
|
---|
| 110 | if (ModelState.IsValid)
|
---|
| 111 | {
|
---|
| 112 | // This doesn't count login failures towards account lockout
|
---|
| 113 | // To enable password failures to trigger account lockout, set lockoutOnFailure: true
|
---|
| 114 | var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);
|
---|
| 115 | if (result.Succeeded)
|
---|
| 116 | {
|
---|
| 117 | _logger.LogInformation("User logged in.");
|
---|
| 118 | return LocalRedirect(returnUrl);
|
---|
| 119 | }
|
---|
| 120 | if (result.RequiresTwoFactor)
|
---|
| 121 | {
|
---|
| 122 | return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
|
---|
| 123 | }
|
---|
| 124 | if (result.IsLockedOut)
|
---|
| 125 | {
|
---|
| 126 | _logger.LogWarning("User account locked out.");
|
---|
| 127 | return RedirectToPage("./Lockout");
|
---|
| 128 | }
|
---|
| 129 | else
|
---|
| 130 | {
|
---|
| 131 | ModelState.AddModelError(string.Empty, "Invalid login attempt.");
|
---|
| 132 | return Page();
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | // If we got this far, something failed, redisplay form
|
---|
| 137 | return Page();
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|