我曾尝试为密码编写正则表达式:
public class ApplicationUser : IdentityUser, ITimeStamps
{
public const string PasswordRegularExpression = @"admin|password";
// public const string PasswordRegularExpression = @"/admin|password/i"; // Tried this too
// public const string PasswordRegularExpression = @"/(admin|password)/i"; // Tried this too
这超出了正常的Microsoft身份标识:
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = true,
RequireLowercase = false,
RequireUppercase = false,
};
这是我的注册视图模型:
public class RegisterViewModel
{
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
[RegularExpression( ApplicationUser.PasswordRegularExpression, ErrorMessage = "The {0} must contain atleast 1 number and must not contain the word 'admin' or '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; }
}
当有人在密码中使用密码或管理员时,我想在jquery验证中抛出错误。但是,jquery验证似乎没有按预期工作。我可能会缺少什么?
asp.net生成的html如下所示:
<input class="form-control input-validation-error" data-val="true" data-val-length="The Password must be at least 6 characters long." data-val-length-max="100" data-val-length-min="6" data-val-regex="The Password must contain atleast 1 number and must not contain the word 'admin' or 'password'" data-val-regex-pattern="admin|password" data-val-required="The Password field is required." id="Password" name="Password" placeholder="Enter a password" type="password">
测试用例:
旁注:
答案 0 :(得分:3)
根据Regular expression to match a line that doesn't contain a word?的答案,以下正则表达式应匹配任何密码,除非,如果它包含&#34; admin&#34;或&#34;密码&#34;:
^((?!(admin|password)).)*$
我已经在Regex101上创建了一个小提琴,以便在将其添加到您的应用程序之前进行测试。