Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change password length in MVC 5 Membership

Trying to change the default minimum password length to 4 characters. I know, 4!!! Ridiculous, right! Not my call.

Anyway, I've changed it on the RegisterViewModel but that doesn't actually change it. To illustrate I've posted the code below. The ModleState.IsValid returns correctly based on the updated ViewModel. However it then calls UserManager.CreateAsync() which returns False with an error message of "Passwords must be at least 6 characters"

I've followed the steps in this, very, similar post(Change Password...) but it does not work for MVC 5 as far I as I can tell. It still returns the same message.

//
    // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser() { UserName = model.UserName, LastLogin = model.LastLogin };


// This is where it 'fails' on the CreateAsync() call
                    var result = await UserManager.CreateAsync(user, model.Password);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: false);
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            // If we got this far, something failed, redisplay form
            return View(model);
        }
like image 317
Refracted Paladin Avatar asked Nov 01 '13 16:11

Refracted Paladin


People also ask

How do I set the minimum length of a password?

Navigate to Computer configuration > Windows settings > Security settings > Account policies > Password policy. Once here, locate the setting “Minimum Password Length” and double-click on it. From the properties menu that opens, type in the minimum password length you want to apply and click “OK” when you finish.

How can I reset my asp net membership password?

MembershipUser usr = Membership. GetUser(username); string resetPwd = usr. ResetPassword(); usr. ChangePassword(resetPwd, newPassword);

What is the minimum and maximum password length?

Reference. The Minimum password length policy setting determines the least number of characters that can make up a password for a user account. You can set a value of between 1 and 14 characters, or you can establish that no password is required by setting the number of characters to 0.


1 Answers

As you can see UserManager has public property IIdentityValidator<string> PasswordValidator for password validation which is currently initialized in UserManager's constructor with hardcoded parameter this.PasswordValidator = (IIdentityValidator<string>) new MinimumLengthValidator(6);.

You can set this property with MinimumLengthValidator object with required password length.

like image 105
alex k Avatar answered Sep 22 '22 16:09

alex k