2

I've created a fresh .net Core 3.1 solution with ASP.net Identity.

The Register page requires a minimum of 6 characters.

I want to configure these requirements thus

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.Configure<IdentityOptions>(options =>
    {
        options.Password.RequiredLength = 1;
        options.Password.RequireUppercase = true;
        options.Password.RequireLowercase = true;
        options.Password.RequireDigit = false;
        options.Password.RequireNonAlphanumeric = false;
        options.Password.RequiredUniqueChars = 5;
    });

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

    services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
        .AddEntityFrameworkStores<ApplicationDbContext>();


    services.AddControllersWithViews();

    services.AddRazorPages();
}

I've added the password options at the top there, but they are being ignored. 6 to 100 characters are still required to be entered when registering.

I've found many articles, eg: https://andyp.dev/posts/set-password-requirements-net-core-3-1-identity

Saying this should work. There isn't even an error so not sure how to problem solve this.

How can I get this working? Thanks.

6
  • If I am not wrong, you should configure Identity after adding it. So try to AddDefaultIdentity then Configure<IdentityOptions>. If that doesn't work, maybe try to AddDbContext before these 2. This is a nice link. Commented May 9, 2020 at 11:49
  • 1
    I did that originally. None of the things you suggest fix it. Commented May 9, 2020 at 11:50
  • I'll copy chunks of code out of that link and report back Commented May 9, 2020 at 11:52
  • 1
    Hmm even when I copy the entire ConfigureServices method out of your link, it still gets stuck on a 6 character RequiredLength even though I set it to 1. If I set it to 7 it also stays stuck on 6, as per my screen grab. Commented May 9, 2020 at 11:54
  • 1
    Turns out it was the viewmodel that had validation on it DOH Commented Jun 16, 2022 at 12:55

3 Answers 3

2

Hi @niico this identity password option will work when you submit the form and and Createasync function will return false. and it will jump to the

foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }

and bind the validation message and return to the page and will show in the top.

Thanks

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

Comments

0

A simple thing that you can double-check, is if you are indeed passing the password when creating the user. If you do not pass the password, Identity doesn't run any of its password validation rules, but still creates the user without errors.

 result = await _userManager.CreateAsync(user, Input.Password);

Comments

0

This limit (6 to 100 chars) is hardcoded in source of register page.

\Identity\Pages\Account\Register.cshtml.cs

You can edit the page afrer scaffold Identity templates.

[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
public string Password { get; set; }

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.