liveBook · Manning

Password policy

As explained in Pro ASP.NET Core 7, Tenth Edition

A password policy is a set of rules specifying the required characteristics of user passwords.

A password policy defines the password characteristics ASP.NET Core Identity accepts when creating users. Validation failures return through IdentityResult and can appear in the model validation summary. section 38.3.3

Configure the policy through IdentityOptions.Password, which exposes PasswordOptions properties:

  • RequiredLength sets the minimum password length. section 38.3.3
  • RequireNonAlphanumeric, RequireLowercase, RequireUppercase, and RequireDigit require specific character categories when enabled. section 38.3.3

Relaxing validation for a demonstration

Configure a six-character minimum, then disable nonalphanumeric, case, and digit requirements. After restarting the application, the password "secret" is accepted when creating a user; apply such a weak policy cautiously in real projects. section 38.3.3

Listing 38.14 Changing validation in the Program.cs file in the Advanced project
...
builder.Services.Configure<IdentityOptions>(opts => {
    opts.Password.RequiredLength = 6;
    opts.Password.RequireNonAlphanumeric = false;
    opts.Password.RequireLowercase = false;
    opts.Password.RequireUppercase = false;
    opts.Password.RequireDigit = false;
    opts.User.RequireUniqueEmail = true;
    opts.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyz";
});
...

Inspect the complete Program.cs configuration, including password settings alongside username and email validation. section 38.3.3

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains configurable password constraints and demonstrates changing them, but does not descend into validator internals.
Examples
Uses the password "secret" to show rejection under one policy and acceptance after relaxing requirements.
What it assumes
Assumes ASP.NET Core Identity user creation is configured and introduces password settings through the options pattern.
What you can do after
Operate/configure password validation through IdentityOptions in Program.cs and observe errors during user creation.
How it connects
Connects password validation to ASP.NET Core Identity, IdentityOptions, PasswordOptions, IdentityResult, and model validation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.