【问题标题】:Why DataType.Password validates only on server-side?为什么 DataType.Password 仅在服务器端验证?
【发布时间】:2014-09-30 15:25:51
【问题描述】:

我有模型

[Required]
[EmailAddress]
[Remote("EmailValidation", "Account", ErrorMessage = "{0} already has an account, please enter a different email address.")]
[Display(Name = "Email")]
public string Email { get; set; }

[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }

[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }

及报名表格:

<form id="registrationForm">
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, String.Empty, new { @class = "text-danger text-center" })
    @Html.HiddenFor(m => m.ForModal, new { @Value = true })
    <div class="form-group text-center">
        <div class="input-group margin-top-10 margin-bottom-5">
            <span class="input-group-addon"><i class="fa fa-user"></i></span>
            @Html.TextBoxFor(m => m.Email, new { @class = "form-control", placeholder = "Email", @readonly = "readonly" })
        </div>
        @Html.ValidationMessageFor(m => m.Email, null, new { @class = "text-danger" })
        <div class="input-group margin-top-10 margin-bottom-5">
            <span class="input-group-addon"><i class="fa fa-lock"></i></span>
            @Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = "Password" })
        </div>
        @Html.ValidationMessageFor(m => m.Password, null, new { @class = "text-danger" })
        <div class="input-group margin-top-10 margin-bottom-5">
            <span class="input-group-addon"><i class="fa fa-lock"></i></span>
            @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control", placeholder = "Confirm Password" })
        </div>
        @Html.ValidationMessageFor(m => m.ConfirmPassword, null, new { @class = "text-danger" })
    </div>
    <div class="text-right">
        <button type="submit" class="btn-u btn-u-orange btn-u-wide" title="Join">Join</button>
    </div>
</form>

当我输入短密码时-验证触发,当我输入与密码不同的confirPassword时-验证触发,但是当我只输入数字为123456时,客户端什么都没有发生,只是在服务器端,毕竟我得到了错误。

http://i.imgur.com/jY8bymy.png(截图链接)

为什么我可以在客户端获得验证 DataType.Password?


当我在互联网上搜索此类问题时,我会得到类似use Html.PasswordFor or Html.EditorFor instead of Html.TextBoxFor 的解决方案。

【问题讨论】:

  • 所需的验证是否在工作客户端?
  • @Batavia,是的。你可以在截图上看到它i.imgur.com/jY8bymy.png

标签: c# .net asp.net-mvc validation data-annotations


【解决方案1】:

DataType 属性不用于验证,它主要用于渲染。例如,如果您使用EditorFor 在内部呈现一个属性,它将使用DataType 属性来确定它应该呈现什么类型的输入,即&lt;input type="password" ... /&gt;。 PasswordFor 基本上做同样的事情,但不需要 DataType 属性。

您的长度验证在客户端工作,因为这是由StringLength 属性而不是DataType 处理的。如果您想强制执行特定的数据类型验证,我建议您考虑使用RegularExpressionAttribute,除此之外您将考虑实现自己的custom validator。

【讨论】:

  • 那么在服务器端验证密码是什么?
  • 据我所知,您的密码有 2 条验证规则 - 它不能为空且必须至少为 6 个字符长,没有其他验证(除非您在后端我们看不到)。但是,错误消息本身会表明您的密码甚至没有到达服务器,因为验证错误表明它丢失了。
  • 比较验证器是应该进行比较的,在这种情况下没有理由不能在客户端完成
  • @Batavia 我的回答不是在谈论比较,当他们只输入数字时,OP 正在谈论没有客户端验证正在触发......原因是因为没有验证为此制定了规则。就验证器而言,123456 既是一个值又是一个至少 6 个字符长的值,因此被认为是可以接受的。
  • @James,好吧,对不起,你是对的! “验证”来自 AspNet.Identity.Core 的 UserManager。
猜你喜欢
  • 2019-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-10
相关资源
最近更新 更多