Jimmyt1988
Jimmyt1988

Reputation: 21186

Temporarily disable the requirement of a password

I am migrating user accounts over to an application that requires 6 characters in a password minimum.

If I run the following function

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

It will ofcourse complain the password is not valid. Is there a way I can temporarily ignore the password requirement so I can create my users?

Notes:

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

This unfortunately does not work:

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

As it states "Your password length must be atleast -1 characters"

Upvotes: 3

Views: 708

Answers (2)

gpro
gpro

Reputation: 592

With IdentityServer4 there is a IList of validators. I had to disable temporary so I did:

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

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

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

Upvotes: 1

Jimmyt1988
Jimmyt1988

Reputation: 21186

Thanks @trailmax

Ah, this seems to do it fine!

this._userManager.PasswordValidator = new CustomPasswordValidator();

and the validator...

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

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

Upvotes: 5

Related Questions