我需要根据以下要求为用户密码编写一些验证规则。 C# ASP.NET MVC。
Passwords must be 6 - 8 characters
Must include at least one character each from at least three of the following categories:
- 大写字母
- 小写字母
- 数字
- 非字母数字字符(例如,!@#$%...)
Must not contain any sequence of 3 or more characters in common with the username
Must not repeat any of the previous 1 passwords
Must be changed if the password is believed to be compromised in any way
目前,我使用 if 语句和循环(尤其是 3 个字符与用户名部分的顺序)编写了一堆非常困惑的验证规则,这些规则目前可以正常使用,但感觉是错误的。我可以采取更好的方法吗?
谢谢
最佳答案
我写了一篇与您所描述的非常相似的文章。它们可以作为正则表达式完成,完成后(至少对我自己而言)这是一项非常有益的成就。
要完成此操作,您将需要使用称为前瞻的正则表达式功能。参见 the information在 regular-expression.info 上所有血腥细节的网站。
您需要的第二件事是实时正则表达式测试器,以帮助您制作正则表达式原型(prototype)。我建议你看看 Rubular .创建几个应该有效的密码,以及一些不应该有效的密码,然后从那里开始。
编辑: 详细说明我的上述评论。并非您的每一个要求都可以或应该通过正则表达式来解决。即,您列出的要求:
- Must not contain any sequence of 3 or more characters in common with the username
- Must not repeat any of the previous 1 passwords
- Must be changed if the password is believed to be compromised in any way
可能应该与主密码验证正则表达式分开处理,因为它们是高度上下文相关的。 “与用户名相同的 3 个或更多字符的序列”可能可以在客户端处理。但是,其他两项可能最好留在服务器端处理。
关于asp.net-mvc - 密码验证(正则表达式?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4116147/