0

I am building a small web application where I am using Identity for Authentication and Authorization of users of my website. The problem I am currently facing is that when I go to Register page (make a GET request), values for Email and Password are already filled (which is already very weird). First of all I have no idea from where those values are coming from and secondly I have tried to use ModelState.Clear() in the OnGetAsync(). But it still does not works. Every time I load the page out of all the form fields Email and Password are prefilled. I would add some screenshots so you guys could see whats going on.

Here is Input model in my Register.cshtml.cs which is used in my Register.cshtml

public class InputModel
        {
            [MaxLength(15)]
            [Display(Name = "First Name")]
            public string FirstName { get; set; }
            [Display(Name = "Last Name")]
            [MaxLength(15)]
            public string LastName { get; set; }
            [Required]
            public string City { get; set; }
            [Required]
            public string State { get; set; }
            [Required]
            public string PhoneNumber { get; set; }
            [Required]
            [EmailAddress]
            [Display(Name = "Email")]
            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)]
            [Display(Name = "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; }
        }

Below is the OnGetAsync() in Register.cshtml.cs

public async Task OnGetAsync(string returnUrl = null)
        {
            ReturnUrl = returnUrl;
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            ModelState.Clear();
        }

Below is Register.cshtml

@page
@model RegisterModel
@{
    ViewData["Title"] = "Register";
}

<h1>@ViewData["Title"]</h1>
<div class="row">
    <div class="col-md-4">
        <form asp-route-returnUrl="@Model.ReturnUrl" method="post">
            <h4>Create a new account.</h4>
            <hr />
            <div asp-validation-summary="All" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Input.FirstName"></label>
                <input class="form-control" type="text" asp-for="Input.FirstName" />
                <span class="text-danger" asp-validation-for="Input.FirstName"></span>
            </div>
            <div class="form-group">
                <label asp-for="Input.LastName"></label>
                <input class="form-control" type="text" asp-for="Input.LastName" />
                <span class="text-danger" asp-validation-for="Input.LastName"></span>
            </div>
            <div class="form-group">
                <label asp-for="Input.City"></label>
                <input class="form-control" type="text" asp-for="Input.City"/>
                <span class="text-danger" asp-validation-for="Input.City"></span>
            </div>
            <div class="form-group">
                <label asp-for="Input.State"></label>
                <input class="form-control" type="text" asp-for="Input.State"/>
                <span class="text-danger" asp-validation-for="Input.State"></span>
            </div>
            <div class="form-group">
                <label asp-for="Input.PhoneNumber"></label>
                <input class="form-control" type="text" asp-for="Input.PhoneNumber" />
                <span class="text-danger" asp-validation-for="Input.PhoneNumber"></span>
            </div>
            <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">Register</button>
            <p>Already have an account? Click <a asp-page="Login" asp-area="Identity">Here</a> to login</p>
        </form>
    </div>
</div>

@section Scripts {
    <partial name="_ValidationScriptsPartial" />
}

EDIT When I delete the Email and Password div(s) from the Register.cshtml page, the values are then displayed into PhoneNumber and ConfirmPassword input tags. As you can see Email and Password fields are prefilled with incorrect data

  • 1
    Basically this is your browser assuming the page is a login form and it populates the "remembered" login information. Take a look at this question https://stackoverflow.com/questions/23646953/stop-browser-from-auto-filling-the-form-username-and-password-fields – Oggy Feb 03 '21 at 22:07
  • Did you try `autocomplete="off"`? Whether the input box value is the other website form's value, or the current website? – Karney. Feb 04 '21 at 05:34
  • @Karney. I did try `autocomplete=off` but it still shows me the values the input fields.. – Usman_Codes._. Feb 04 '21 at 07:07
  • @Usman_Codes._., will it happen in other browsers? The browser could have opened the auto-filled in setting. `ModelState.Clear()` can clear the `[BindProperty]` value, but it could not clear the browser's form value. – Karney. Feb 04 '21 at 07:12
  • Well I cleared the passwords and emails that were saved during development on the `localhost` and now the values are not being showed up. – Usman_Codes._. Feb 04 '21 at 10:18

1 Answers1

0

To anyone in the future facing this problem, there is likely nothing wrong with your code but you might need to clear the already saved passwords and usernames by the localhost in the browser (if you have any). You can also try changing the port your app is running on because it is possible that some previous project running on the same port as your current project has saved something (likely a username or password) in the browser which is conflicting with you current application.