gpt4 book ai didi

c# - 最大长度条件的 Asp.net Identity PasswordValidator

转载 作者:太空宇宙 更新时间:2023-11-03 18:27:24 25 4
gpt4 key购买 nike

我遇到了非典型情况来验证最大长度要求的密码。我正在尝试调整密码验证器以满足我的要求,但密码的最大长度是我遇到的问题。这是我的密码验证器的样子。

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:

自定义密码验证器:

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;
}
}

关于c# - 最大长度条件的 Asp.net Identity PasswordValidator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31249823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com