暂时禁用密码要求

时间:2015-04-30 13:06:53

标签: c# owin asp.net-identity-2

我正在将用户帐户迁移到最少需要6个字符的应用程序。

如果我运行以下功能

var result = await UserManager.CreateAsync(newUser, "");

当然会抱怨密码无效。 有没有办法可以暂时忽略密码要求,以便创建用户?

备注:

private ApplicationUserManager _userManager;
    public ApplicationUserManager UserManager
    {
        get
        {
            if (_userManager == null)
            {
                this._userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            return _userManager;
        }
        private set
        {
            _userManager = value;
        }
    }

遗憾的是,这不起作用:

this._userManager.PasswordValidator = new PasswordValidator
{
    RequiredLength = -1, // I've also tried 0
    RequireNonLetterOrDigit = false,
    RequireDigit = false,
    RequireLowercase = false,
    RequireUppercase = false,
};

正如它所述&#34;您的密码长度必须至少为-1个字符&#34;

2 个答案:

答案 0 :(得分:5)

谢谢@trailmax

啊,这似乎做得很好!

this._userManager.PasswordValidator = new CustomPasswordValidator();

和验证器...

public class CustomPasswordValidator : IIdentityValidator<string>
{
    public CustomPasswordValidator()
    {
    }

    public Task<IdentityResult> ValidateAsync(string item)
    {
        return Task.FromResult(IdentityResult.Success);
    }
}

答案 1 :(得分:0)

使用 IdentityServer4 ,有IList个验证器。我不得不禁用临时的,所以我做了:

var pwValidators = userManager.PasswordValidators.ToList();
userManager.PasswordValidators.Clear(); 

addPaswordResult = await userManager.AddPasswordAsync(user, password);

pwValidators.ForEach(x => userManager.PasswordValidators.Add(x));