locked
Problems with Identity RRS feed

  • Question

  • User389708749 posted

    Hi,

    I am using the page ResetPassword out of the box from the Identity scaffolding.

    When I call the page it correctly copies the code to the class "InputModel" but after the user fills out the form, it comes back with all the fields, except code is null.

    Since it need to have the code to validate, it fails.

    I am pretty sure I have the out of the box code, but I will post it just in case.

    What do I need to do to get the Code to not be null iin the postback?  It works on the Get...

    thanks

    @page
    @using ...
    @model ResetPasswordModel
    @{
        ViewData["Title"] = "Reset password";
    }
    
    <h1>@ViewData["Title"]</h1>
    <h4>Reset your password.</h4>
    <hr />
    <div class="row">
        <div class="col-md-4">
            <form method="post">
                <div asp-validation-summary="All" class="text-danger"></div>
                <input asp-for="Input.Code" type="hidden" />
                <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>
                <div class="form-group">
                    <label asp-for="Input.Password"></label>
                    <input asp-for="Input.Password" class="form-control" />
                    <span asp-validation-for="Input.Password" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Input.ConfirmPassword"></label>
                    <input asp-for="Input.ConfirmPassword" class="form-control" />
                    <span asp-validation-for="Input.ConfirmPassword" class="text-danger"></span>
                </div>
                <button type="submit" class="btn btn-primary">Reset</button>
            </form>
        </div>
    </div>
    
    @section Scripts {
        <partial name="_ValidationScriptsPartial" />
    }
    



    [AllowAnonymous]
        public class ResetPasswordModel : PageModel
        {
            private readonly UserManager<IdentityUser> _userManager;
    
            public ResetPasswordModel(UserManager<IdentityUser> userManager)
            {
                _userManager = userManager;
            }
    
            [BindProperty]
            public InputModel Input { get; set; }
    
            public class InputModel
            {
                [Required]
                [EmailAddress]
                public string Email { get; set; }
    
                [Required]
                [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
                [DataType(DataType.Password)]
                public string Password { get; set; }
    
                [DataType(DataType.Password)]
                [Display(Name = "Confirm password")]
                [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
                public string ConfirmPassword { get; set; }
    
                [FromQuery(Name = "userId")]
                public string Code { get; set; }
            }
    
            public IActionResult OnGet(string code = null)
            {
                if (code == null)
                {
                    return BadRequest("A code must be supplied for password reset.");
                }
                else
                {
                    Input = new InputModel
                    {
                        Code = code
                    };
                    return Page();
                }
            }
    
            public async Task<IActionResult> OnPostAsync()
            {
                if (!ModelState.IsValid)
                {
                    return Page();
                }
    
                var user = await _userManager.FindByEmailAsync(Input.Email);
                if (user == null)
                {
                    // Don't reveal that the user does not exist
                    return RedirectToPage("./ResetPasswordConfirmation");
                }
    
                var result = await _userManager.ResetPasswordAsync(user, Input.Code, Input.Password);
                if (result.Succeeded)
                {
                    return RedirectToPage("./ResetPasswordConfirmation");
                }
    
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
                return Page();
            }
        }

    Tuesday, April 20, 2021 1:04 AM

Answers

  • User-1330468790 posted

    Hi BlazorFan,

     

    Have you changed the logic for generating code

    If not, why you add '[FromQuery(Name = "userId")]' this attribute on Code property?

     [FromQuery(Name = "userId")]
     public string Code { get; set; }

      

    You should remove the [FromQuery] attribute from "Code" property of the "InputModel" class. Otherwise, it will keep fetching the code from query string, which will cause this failure => the value will be in forms when you are doing a post request.

     

    Hope help.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 20, 2021 2:42 AM

All replies

  • User-1330468790 posted

    Hi BlazorFan,

     

    Have you changed the logic for generating code

    If not, why you add '[FromQuery(Name = "userId")]' this attribute on Code property?

     [FromQuery(Name = "userId")]
     public string Code { get; set; }

      

    You should remove the [FromQuery] attribute from "Code" property of the "InputModel" class. Otherwise, it will keep fetching the code from query string, which will cause this failure => the value will be in forms when you are doing a post request.

     

    Hope help.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 20, 2021 2:42 AM
  • User389708749 posted

    Sean this worked,

    Thank you and much appreciated

    Tuesday, April 20, 2021 2:09 PM