markoverflow
markoverflow

Reputation: 123

ASP.NET MVC Identity password RequiredLength

How can I make RequiredLength of identity to work. I already try to change the value of RequiredLength to 10, but i still can register using 6 password. when i try to change it to RequiredLength to 5, it doesnt work. It said password need to be atleast 6 length. I also modified the viewmodels but same thing happen, I think it was stuck in RequiredLength = 6. I also try to clean and rebuild my project.

manager.PasswordValidator = new PasswordValidator
{
    RequiredLength = 10,
    RequireNonLetterOrDigit = false,
    RequireDigit = false,
    RequireLowercase = false,
    RequireUppercase = false,
};


EDIT: working on register account, but not working on change password

Upvotes: 3

Views: 1044

Answers (3)

java-love
java-love

Reputation: 515

in the AccountViewModels.cs at the RegisterViewModel class, by the attribute:

[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]

change to:

[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 10)]

and also change it at the ResetPasswordViewModel

Upvotes: 1

Ryan Searle
Ryan Searle

Reputation: 1627

In your ViewModel add an attribute to your Password property:

[MinLength(10,ErrorMessage = "Minimum length of 10 characters is required")]
public string Password { get; set; }

On your post method in the controller check for ModelState.IsValid:

public ActionResult Login(YourLoginViewModel model)
{
    if (ModelState.IsValid)
    {
        //Your Login logic here
    }
    else
    {
        return View(model)
    }
}

Finally in your view add ValidationSummary to display error:

@Html.ValidationSummary("")

Upvotes: 1

Chandru  velan
Chandru velan

Reputation: 146

Hi you can use Data Annotation Feature to achieve by decorating the entity field with validator.

[Required]
[StringLength(10)]
public string RequiredLength { get; set; } 

Upvotes: 1

Related Questions