![](/img/trans.png)
[英]WCF Custom Username and Password Validator using a Repository Database c#
[英]How to build custom C# password validator using Delegates?
我想使用 C# 代表构建自己的密码验证器。
我的目标是实现类似于管道的东西,通过该管道将验证密码。
public class Validator
{
public static bool IsPasswordValid() =>
ValidatePassword("password", (options) =>
{
options.HasMinLength(8);
options.HasUpperCase();
});
public static bool ValidatePassword(string password, Action<ValidatorOptions> input)
{
// Can I somehow call the defined pipelien here and validate the password?
throw new NotImplementedException();
}
}
public class ValidatorOptions
{
public bool HasUpperCase() => true;
public bool HasMinLength(int num) => true;
}
我的 go 方向正确吗? 甚至可能吗?
更新
ValidatorOptions
class 将有许多验证方法,例如 HasDigit()、HasLowerCase()、HasSpecialCharacter(5) 等。顺便说一句,这些方法将在其正文中使用正则表达式。
此选项将与ValidatePassword
方法一起发布为 NuGet package。 我想使用这个 package 来构建动态管道,其中包含我目前对给定项目感兴趣的验证选项。
ValidatePassword
方法应该通过作为参数传递的“验证器管道”来验证密码。 如何实现ValidatePassword
方法?
感谢 Marc Gravell,我已经找到了解决方案。
我不得不将Action
替换为Func
并将构造函数添加到ValidatorOptions
class。
public class Validator
{
public static bool IsPasswordValid() =>
ValidatePassword("password", options => options.HasMinLength(8)
&& options.HasUpperChar());
public static bool ValidatePassword(string password, Func<ValidatorOptions, bool> input)
{
var options = new ValidatorOptions(password);
return input(options)
}
}
public class ValidatorOptions
{
public string Input { get; }
public ValidatorOptions(string input) => Input = input;
public bool HasUpperChar() => new Regex(@"[A-Z]+").IsMatch(Input);
public bool HasMinLength(int length) => Input.lengt >= length;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.