Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 714
markoverflow Avatar asked Nov 17 '25 15:11

markoverflow


2 Answers

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

[Required]
[StringLength(10)]
public string RequiredLength { get; set; } 
like image 150
Chandru velan Avatar answered Nov 20 '25 04:11

Chandru velan


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("")
like image 20
Ryan Searle Avatar answered Nov 20 '25 05:11

Ryan Searle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!