PasswordOptions

Identity 中通过 PasswordOptions 进行密码配置。

PasswordOptions 源码:

  1. public class PasswordOptions
  2. {
  3. /// <summary>
  4. /// Gets or sets the minimum length a password must be. Defaults to 6.
  5. /// </summary>
  6. public int RequiredLength { get; set; } = 6;
  7. /// <summary>
  8. /// Gets or sets the minimum number of unique chars a password must comprised of. Defaults to 1.
  9. /// </summary>
  10. public int RequiredUniqueChars { get; set; } = 1;
  11. /// <summary>
  12. /// Gets or sets a flag indicating if passwords must contain a non-alphanumeric character. Defaults to true.
  13. /// </summary>
  14. /// <value>True if passwords must contain a non-alphanumeric character, otherwise false.</value>
  15. public bool RequireNonAlphanumeric { get; set; } = true;
  16. /// <summary>
  17. /// Gets or sets a flag indicating if passwords must contain a lower case ASCII character. Defaults to true.
  18. /// </summary>
  19. /// <value>True if passwords must contain a lower case ASCII character.</value>
  20. public bool RequireLowercase { get; set; } = true;
  21. /// <summary>
  22. /// Gets or sets a flag indicating if passwords must contain a upper case ASCII character. Defaults to true.
  23. /// </summary>
  24. /// <value>True if passwords must contain a upper case ASCII character.</value>
  25. public bool RequireUppercase { get; set; } = true;
  26. /// <summary>
  27. /// Gets or sets a flag indicating if passwords must contain a digit. Defaults to true.
  28. /// </summary>
  29. /// <value>True if passwords must contain a digit.</value>
  30. public bool RequireDigit { get; set; } = true;
  31. }

配置 PasswordOptions:

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.AddDbContextPool<AppDbContext>(
  4. options => options.UseSqlServer(_configuration.GetConnectionString("StudentDBConnection"))
  5. );
  6. services.Configure<IdentityOptions>(options =>
  7. {
  8. options.Password.RequiredLength = 6;
  9. options.Password.RequireNonAlphanumeric = false;
  10. options.Password.RequireUppercase = false;
  11. });
  12. ...
  13. }