C# 用于最大长度条件的Asp.net标识密码验证程序
我有非典型情况验证密码的最大长度要求。 我试图调整密码验证器,以达到我的要求,但密码的最大长度是我有麻烦的事情。下面是我的密码验证器的外观C# 用于最大长度条件的Asp.net标识密码验证程序,c#,asp.net-identity-2,C#,Asp.net Identity 2,我有非典型情况验证密码的最大长度要求。 我试图调整密码验证器,以达到我的要求,但密码的最大长度是我有麻烦的事情。下面是我的密码验证器的外观 manager.PasswordValidator = new PasswordValidator { RequiredLength = 6, RequireNonLetterOrDigit = false, //Overrode per requirement Requ
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = false, //Overrode per requirement
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
MaxLength = 10 //TODO:Max length requirement
};
有人能帮我吗?看起来我需要定义一些自定义验证器 您需要创建具有所需业务逻辑的自定义密码验证器 然后,需要将ApplicationUserManager属性中的PasswordValidator设置为新CustomPasswordValidator的实例 下面是一些来自默认ASP.NET 5 MVC 6模板的示例代码,但同样适用于MVC 5: CustomPasswordValidator:
您需要创建具有所需业务逻辑的自定义密码验证器 然后,需要将ApplicationUserManager属性中的PasswordValidator设置为新CustomPasswordValidator的实例 下面是一些来自默认ASP.NET 5 MVC 6模板的示例代码,但同样适用于MVC 5: CustomPasswordValidator:
public class CustomPasswordValidator : PasswordValidator
{
public int MaxLength { get; set; }
public override async Task<IdentityResult> ValidateAsync(string item)
{
IdentityResult result = await base.ValidateAsync(item);
var errors = result.Errors.ToList();
if (string.IsNullOrEmpty(item) || item.Length > MaxLength)
{
errors.Add(string.Format("Password length can't exceed {0}", MaxLength));
}
return await Task.FromResult(!errors.Any()
? IdentityResult.Success
: IdentityResult.Failed(errors.ToArray()));
}
}
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new CustomPasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
MaxLength = 10
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
// Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
// You can write your own provider and plug it in here.
manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
{
MessageFormat = "Your security code is {0}"
});
manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
{
Subject = "Security Code",
BodyFormat = "Your security code is {0}"
});
manager.EmailService = new EmailService();
manager.SmsService = new SmsService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}