ASP NET 4 如何使用具有 MinLength 属性的数据类型密码的数据注释?

发布于 2024-11-15 00:05:47 字数 242 浏览 3 评论 0原文

public class ChangePasswordObject {

    [Required] [DataType(DataType.EmailAddress)]
    string email;
    [Required]
    string authorization_code;
    [Required] [DataType(DataType.Password)]
    string password;
}
public class ChangePasswordObject {

    [Required] [DataType(DataType.EmailAddress)]
    string email;
    [Required]
    string authorization_code;
    [Required] [DataType(DataType.Password)]
    string password;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

树深时见影 2024-11-22 00:05:47

应该很简单:

[Required] 
[DataType(DataType.Password)]
[StringLength(20, MinimumLength = 3)]
string password;

StringLength 的第一个参数是最大长度。


现在我的 0.02 美元:

正如评论中所述,对密码字段提供最小和最大限制可以告诉攻击者很多有关您密码要求的信息,他们可以根据此信息优化攻击。

另外,在存储和传递明文密码时要小心——您应该使用单向加密算法和随机盐尽快对它们进行加盐+散列。验证密码应该对用户的输入重复加密,使用已知的盐并比较生成的哈希值。如果您使用明文密码执行的操作多于发布密码的操作,则您可能需要重新考虑您的安全策略。

Should be as easy as:

[Required] 
[DataType(DataType.Password)]
[StringLength(20, MinimumLength = 3)]
string password;

The first parameter to StringLength is the maximum length.


Now for my $0.02:

As noted in the comments, providing minimum and maximum constraints on your password fields tells an attacker a lot about your password requirements, and they could optimize their attack based on this information.

Also, be careful about storing and passing around plaintext passwords -- you should salt+hash them ASAP using a one-way encryption algorithm and a random salt. Verifying passwords should repeat the encryption on the user's input ,using the known salt and comparing the resulting hashes. If you're doing more with a plaintext password than POSTing it, you may want to rethink your security strategy.

橘亓 2024-11-22 00:05:47

将 StringLength 属性添加到密码字段。示例此处 - 位于底部页面的。

Add a StringLength attribute to the password field. An example here - at the bottom of the page.

十级心震 2024-11-22 00:05:47
[MembershipPasswordAttribute(MinRequiredNonAlphanumericCharacters = 4, MinRequiredPasswordLength = 7, MinNonAlphanumericCharactersError = "Alpha", MinPasswordLengthError = "MIN Length")]
[DataType(DataType.Password)]
public string Password { get; set; }

[System.ComponentModel.DataAnnotations.Compare("Password",ErrorMessage  ="{0} and {1} should be same")]
public string ComparePassword { get; set; }
[MembershipPasswordAttribute(MinRequiredNonAlphanumericCharacters = 4, MinRequiredPasswordLength = 7, MinNonAlphanumericCharactersError = "Alpha", MinPasswordLengthError = "MIN Length")]
[DataType(DataType.Password)]
public string Password { get; set; }

[System.ComponentModel.DataAnnotations.Compare("Password",ErrorMessage  ="{0} and {1} should be same")]
public string ComparePassword { get; set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文