【问题标题】:ASP.NET core 3.1 rendering a form with Incorrect values for Input fields on GET requestASP.NET 核心 3.1 在 GET 请求上呈现输入字段的值不正确的表单
【发布时间】:2023-04-08 18:12:01
【问题描述】:

我正在构建一个小型 Web 应用程序,我在其中使用 Identity 对我的网站用户进行身份验证和授权。我目前面临的问题是,当我转到Register 页面(发出GET 请求)时,EmailPassword 的值已经被填充(这已经很奇怪了)。首先我不知道这些值是从哪里来的,其次我尝试在OnGetAsync() 中使用ModelState.Clear()。但它仍然不起作用。每次我从所有表单字段中加载页面时,EmailPassword 都会预先填写。我会添加一些屏幕截图,以便你们可以看到发生了什么。

这是我的Register.cshtml.cs 中的Input 模型,它在我的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; }
        }

以下是Register.cshtml.cs中的OnGetAsync()

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

下面是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" />
}

编辑 当我从Register.cshtml 页面中删除EmailPassword div(s) 时,这些值会显示到PhoneNumberConfirmPassword 输入标签中。

【问题讨论】:

  • 基本上这是你的浏览器,假设页面是一个登录表单,它会填充“记住的”登录信息。看看这个问题stackoverflow.com/questions/23646953/…
  • 你试过autocomplete="off"吗?输入框值是其他网站表单的值,还是当前网站的值?
  • @Karney。我确实尝试过autocomplete=off,但它仍然向我显示输入字段的值..
  • @Usman_Codes._.,它会在其他浏览器中发生吗?浏览器可能已经打开了自动填充设置。 ModelState.Clear()可以清除[BindProperty]的值,但是不能清除浏览器的form值。
  • 好吧,我清除了在开发过程中在localhost 上保存的密码和电子邮件,现在这些值没有显示出来。

标签: c# forms asp.net-core asp.net-identity


【解决方案1】:

对于将来遇到此问题的任何人,您的代码可能没有任何问题,但您可能需要通过浏览器中的localhost 清除已保存的passwordsusernames(如果有的话) .您还可以尝试更改您的应用程序正在运行的port,因为在与当前项目相同的端口上运行的某些先前项目可能在浏览器中保存了某些内容(可能是usernamepassword)与您当前的应用程序冲突。

【讨论】:

    猜你喜欢
    • 2013-09-01
    • 2020-06-07
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 2012-06-07
    • 2019-02-17
    相关资源
    最近更新 更多