Irfan Y
Irfan Y

Reputation: 1310

Change password validation in asp.net identity

I want my password field to accept empty string. I am using database first approach and I have changed the definition of AspNetUsers table to allow Null for PasswordHash. But during Register It says password must be 6 characters. How can I remove this validation. My project does not contain IdentityConfig.cs class. I have recently updated Identity to 2.2.1. I am using MVC.

Update

I have written this function in AccountController.

[HttpPost]
        [AllowAnonymous]
        public async Task<JsonResult> RegisterUser(string email, string password = "")
        {
            var user = new ApplicationUser() { UserName = email };
            var result = await UserManager.CreateAsync(user, password);
            if (result.Succeeded)
            {
                await SignInAsync(user, isPersistent: true);
                return Json("Done", JsonRequestBehavior.AllowGet);
            }
            return Json("Error", JsonRequestBehavior.AllowGet);

        }

I get validation error in result variable.

Upvotes: 0

Views: 4269

Answers (2)

Ganesh PMP
Ganesh PMP

Reputation: 51

@Irfan Yousanif, since you dont have IdentityConfig.cs class, you can write as below my sample code in a sample scenario :

    var userStore = new UserStore<IdentityUser>();
        var manager = new UserManager<IdentityUser>(userStore);

        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 8, 
            RequireNonLetterOrDigit = true,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };


        var user = new IdentityUser() { UserName = UserName.Text };
        IdentityResult result = manager.Create(user, Password.Text);

if you want to remove entire validation, please comment as below:

manager.PasswordValidator = new PasswordValidator
        {
            //RequiredLength = 8, 
            //RequireNonLetterOrDigit = true,
            //RequireDigit = true,
            //RequireLowercase = true,
            //RequireUppercase = true,
        };

Upvotes: 0

Siva Gopal
Siva Gopal

Reputation: 3502

If you look at: \App_Start\IdentityConfig.cs, in the "Create" method you see, RequiredLength = 6 and you need to modify this:

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
        {
            var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
            // Configure validation logic for usernames
            manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };

            // Configure validation logic for passwords
            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength = 6, //This is the validation you need to modify.
                RequireNonLetterOrDigit = true,
                RequireDigit = true,
                RequireLowercase = true,
                RequireUppercase = true,
            };

            // Configure user lockout defaults
            manager.UserLockoutEnabledByDefault = true;
            manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
            manager.MaxFailedAccessAttemptsBeforeLockout = 5;

...

Hope this help you...

Upvotes: 2

Related Questions