因为默认的验证过于严格,不适合中国环境。所以按下面的代码修改:

修改

1
2
3
4
5
6
7
8
9
services.Configure<IdentityOptions>(options =>
 {
   options.Password.RequireDigit = false;
   options.Password.RequireLowercase = true;
   options.Password.RequireUppercase = true;
   options.Password.RequireNonAlphanumeric = false;
   options.Password.RequiredUniqueChars = 3;
   options.Password.RequiredLength = 6;
 });

使用外部配置文件

这样做的目的是方便不同环境切换配置文件

  • Startup.cs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public IConfiguration Configuration { get; }

public Startup(IHostingEnvironment env)
{
  var builder = new ConfigurationBuilder()
      .SetBasePath(env.ContentRootPath)
      .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
      .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
      .AddEnvironmentVariables();

  Configuration = builder.Build();
}


public void ConfigureServices(IServiceCollection services)
{
    ...
    services.Configure<IdentityOptions>(options =>
    {
    Configuration.GetSection("IdentityOptions").Bind(options);
    });
    ...
});
}
  • appsetting.json
1
2
3
4
5
6
7
8
9
  "IdentityOptions": {
   "Password": {
     "RequireDigit": false,
     "RequireLowercase": true,
     "RequireUppercase": false,
     "RequireNonAlphanumeric": false,
     "RequiredUniqueChars": 3,
     "RequiredLength": 6
   }