Wayne
Wayne

Reputation: 3519

Altering ASP.NET Core Services DefaultIdentity does not alter the password options

After Scaffolding Identity Authentication, I am having difficulty altering the Identity Options.

I used this command:

dotnet new webapp --auth Individual -o mywebapp

Then furthermore I altered the Startup.cs file as such, altering the Identity options:

 public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlite(
                Configuration.GetConnectionString("DefaultConnection")));

        services.AddDefaultIdentity<IdentityUser>(config =>
        {
            // TODO
            //config.SignIn.RequireConfirmedEmail = true;
        })
            .AddDefaultUI(UIFramework.Bootstrap4)
            .AddEntityFrameworkStores<ApplicationDbContext>();


        services.Configure<IdentityOptions>(options =>
        {
            // Password settings
            options.Password.RequireDigit = true;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = false;
            options.Password.RequireLowercase = false;
            options.Password.RequiredLength = 3;
            options.Password.RequiredUniqueChars = 1;

            // Lockout settings
            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
            options.Lockout.MaxFailedAccessAttempts = 5;
            options.Lockout.AllowedForNewUsers = true;

            // User settings
            options.User.RequireUniqueEmail = true;
        });

Setting options.Password.RequiredLength has no effect.

When I run the application the browser shows an error message:

The Password must be at least 6 and at max 100 characters long.

I am not expecting the error "least 6"; From what I read it should be "3". How do I fix this?

So much black magic is happening, and searching for the frase "at least" reveals nothing.

Edit ---> Adding the ViewModel Code that was scaffolded:

namespace netplus.Models.Account
{
   public class RegisterViewModel
   {
      [Required, MaxLength(256)]
      public string Username { get; set; }

      [Required, DataType(DataType.Password)]
      public string Password { get; set; }

      [DataType(DataType.Password), Compare(nameof(Password))]
      public string ConfirmPassword { get; set; }
   }
}

Upvotes: 0

Views: 626

Answers (1)

Edward
Edward

Reputation: 29976

For IdentityOptions, it is configured for _userManager.CreateAsync(user, Input.Password) to validate the password.

For The Password must be at least 6 and at max 100 characters long, this is controlled by ViewModel in RegisterModel.

You could not use IdentityOptions to control this client side validation.

Follow steps below to control it:

  1. Right click project ->Add New Scaffold Item-> Identity->Check Account\Register->Select right Data context class
  2. Open RegisterModel and modify the InputModel for client validation

    public class InputModel
    {
        [Required]
        [EmailAddress]
        [Display(Name = "Email")]
        public string Email { get; set; }
    
        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }
    
        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }
    

Upvotes: 1

Related Questions