0

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.

12
  • Do you see AccountViewModel class in Models folder? Commented Oct 6, 2015 at 8:11
  • I am not using any ViewModel. Commented Oct 6, 2015 at 8:13
  • So how your tables got generated? I mean when you create a project you must have selected individual user authentication right? Commented Oct 6, 2015 at 8:16
  • yes am just using bootstrap modal on every page for login/register. I have updated question. Please have a look. Commented Oct 6, 2015 at 8:17
  • 1
    If you have selected Individual User Authentication then all those model classes gets generated for you.. Show the view otherwise.. Commented Oct 6, 2015 at 8:19

2 Answers 2

2

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...

Sign up to request clarification or add additional context in comments.

10 Comments

I have mentioned in quesiton. "My project does not contain IdentityConfig.cs class"
@IrfanWattoo Yes I got it. But in that case what is the type of UserManager in your code? If its type is: "ApplicationUserManager", then can you F12 and see where it is defined?
Step Into does not work on that line instead it shows a popup message and performs a step over. However when I hover over UserManager the popup is: Microsoft.aspnet.identity.UserManager<porjectName.Models.ApplicationUser>
Ok. Do F12/GoToDefinition on "UserManager" in "await UserManager.CreateAsync(...)"when application not running. In default MVC application template, it goes to a property defined in AccountController.cs and its type is "ApplicationUserManager" which is defined in IdentityConfig.cs. So just try to find where it is defined in your case since you said you don't have IdentityConfig.cs. Because if a model is associated with it then you should see a validation too.
When I perform step-into the popup message is: Your step-into request in a automatic step-over of a property or operator. This behavior can be overridden in the context menu for the line being executed by choosing "Step Into Specific" or by unchecking option "Step over properties and operators". (Step-into shortcut is F11. Did you mean F11 instead of F12) I have searched CreateAsync in entire solution but search returns nothing.
|
0

@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,
        };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.