如何使用FluentValidation创建强密码?

问题描述

我正在使用FluentValidation设计用户注册屏幕。

我想建立一种控制机制,该机制将提供有关以下所有步骤的信息。

我尝试过的代码

RuleFor(p => p.Password).Matches(@"[A-Z]+").WithMessage("Your password must contain at least one uppercase letter.");
        RuleFor(p => p.Password).Matches(@"[a-z]+").WithMessage("Your password must contain at least one lowercase letter.");
        RuleFor(p => p.Password).Matches(@"[0-9]+").WithMessage("Your password must contain at least one number.");
        RuleFor(x => x.Password).Matches(@"[\!\?\*\.]*$").WithMessage("Your password must contain at least one (!? *.).");

但是我达不到想要的结果。我还查看了FluentValidation文档,但看不到任何有用的示例。

如果您有帮助,我会很高兴。

谢谢。

解决方法

请参考以下示例:

[options]
addons_path = /mnt/extra-addons
logfile = /etc/odoo/odoo-server.log

动作:

public class Login
    { 
        public string Password { get; set; }
    }


     public class PassWordValidator : AbstractValidator<Login>
        {
            public PassWordValidator()
            {
                RuleFor(p => p.Password).NotEmpty().WithMessage("Your password cannot be empty")
                    .MinimumLength(8).WithMessage("Your password length must be at least 8.")
                    .MaximumLength(16).WithMessage("Your password length must not exceed 16.")
                    .Matches(@"[A-Z]+").WithMessage("Your password must contain at least one uppercase letter.")
                    .Matches(@"[a-z]+").WithMessage("Your password must contain at least one lowercase letter.")
                    .Matches(@"[0-9]+").WithMessage("Your password must contain at least one number.")
                    .Matches(@"[\!\?\*\.]+").WithMessage("Your password must contain at least one (!? *.).");
            }
        }

这是邮递员的测试结果:

enter image description here