locked
Login page 'The Username field is required' validation error RRS feed

  • Question

  • User-501297529 posted

    I created a login page thru scaffold identity and it had an Email address instead of username and password. I tried updating email to username from this to 

    <div class="form-group">
                        <label asp-for="Input.Email"></label>
                        <input asp-for="Input.Email" class="form-control" />
                        <span asp-validation-for="Input.Email" class="text-danger"></span>
                    </div>

    to

      <div class="form-group">
                        <label asp-for="User.Identity.Name"></label>
                        <input asp-for="User.Identity.Name" class="form-control" />
                        <span asp-validation-for="User.Identity.Name" class="text-danger"></span>
                    </div>

    I get this validation message when i type in a username and obviously I should get this message if I'm typing in a username.

    AlsoI'm not sure if I'm using the correct Input Model for Username. If not what should that be.

    I also updated the InputModel class and OnPostAsync also and not sure if that is correct.

    public class InputModel
            {
                [Required]
                [DataType(DataType.Text)]            
                public string Username { get; set; }
    
                [Required]
                [DataType(DataType.Password)]
                public string Password { get; set; }
    
                [Display(Name = "Remember me?")]
                public bool RememberMe { get; set; }
            }
     public async Task<IActionResult> OnPostAsync(string returnUrl = null)
            {
                returnUrl = returnUrl ?? Url.Content("~/");
    
                if (ModelState.IsValid)
                {
                    // This doesn't count login failures towards account lockout
                    // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                    var result = await _signInManager.PasswordSignInAsync(User.Identity.Name, Input.Password, Input.RememberMe, lockoutOnFailure: true);
                    if (result.Succeeded)
                    {
                        _logger.LogInformation("User logged in.");
                        return LocalRedirect(returnUrl);
                    }
                    if (result.RequiresTwoFactor)
                    {
                        return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
                    }
                    if (result.IsLockedOut)
                    {
                        _logger.LogWarning("User account locked out.");
                        return RedirectToPage("./Lockout");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                        return Page();
                    }
                }
    
                // If we got this far, something failed, redisplay form
                return Page();
            }



    Wednesday, June 12, 2019 7:27 PM

Answers

  • User475983607 posted

    I also updated the InputModel class and OnPostAsync also and not sure if that is correct.

    It's not correct.  The tag helper and the OnPostAsync must match the InputModel property names.

    <div class="form-group">
    	<label asp-for="Input.Username"></label>
    	<input asp-for="Input.Username" class="form-control" />
    	<span asp-validation-for="Input.Username" class="text-danger"></span>
    </div>
    var result = await _signInManager.PasswordSignInAsync(Input.Username, Input.Password, Input.RememberMe, lockoutOnFailure: true);

    This construct is used after a successful authentication to get the current user's username.

    User.Identity.Name

    I recommend going through https://www.learnrazorpages.com/ before moving forward.  The site is well put together and illustrates many valuable Razor Page patterns.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 12, 2019 8:34 PM