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.