liveBook · Manning

UserOptions

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

UserOptions is the ASP.NET Core Identity options class containing configuration for username and email-address validation.

UserOptions is the IdentityOptions.User configuration object for validating usernames and email addresses during account creation. section 38.3.3

Configure the two available properties:

  • AllowedUserNameCharacters lists every legal username character explicitly; the default permits letters, digits, hyphen, period, underscore, and @. section 38.3.3
  • RequireUniqueEmail rejects new accounts whose email address has already been used when set to true. section 38.3.3

Configure username and email validation

Register Identity, then configure UserOptions through IdentityOptions: builder.Services.Configure<IdentityOptions>(opts => { opts.User.RequireUniqueEmail = true; opts.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyz"; }); Restart the application and request /users/create. A duplicate email and a username such as Bob! produce validation errors. section 38.3.3

Table 38.10 The UserOptions properties (view table figure)

Name

Description

AllowedUserNameCharacters

This string property contains all the legal characters that can be used in a username. The default value specifies a–z, A–Z, and 0–9 and the hyphen, period, underscore, and @ characters. This property is not a regular expression, and every legal character must be specified explicitly in the string.

RequireUniqueEmail

Setting this bool property to true requires new accounts to specify e-mail addresses that have not been used previously.

Use the property table as a compact reference for the supported username and email-validation settings. section 38.3.3

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

How deep it goes
Defines two properties and demonstrates their effects, but does not descend into validator implementation internals.
Examples
Uses Bob!, a duplicate email address, the /users/create route, and Program.cs configuration to demonstrate validation.
What it assumes
Assumes a configured ASP.NET Core Identity application and familiarity with dependency-injection service configuration.
What you can do after
Operate/configure Identity validation by restricting username characters and requiring unique email addresses.
How it connects
Connects IdentityOptions, the options pattern, user creation, and Identity 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.