为什么我的密码验证规则不起作用?

时间:2019-05-29 08:51:35

标签: asp.net-core

我在启动类services.AddIdentity <>部分中配置了密码验证规则,但是它不起作用。我使用了Asp Core Identity API,例如:

Password.RequiredLength = 8  

但是我需要在项目中输入任意长度的密码。

这是我当前文件的内容(即CreateController和StartUp.cs)

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDbContext<AppIdentityDBContext>(options     =>options.UseSqlServer(
            Configuration["ConnectionStrings:IdentityConnection"]
            ));
        services.AddIdentity<AppUser, IdentityRole>(opts => {
              opts.Password.RequiredLength = 8;
              opts.Password.RequireNonAlphanumeric = false;
              opts.Password.RequireLowercase = false;
              opts.Password.RequireUppercase = false;
              opts.Password.RequireDigit = false;
        }).AddEntityFrameworkStores<AppIdentityDBContext>()
        .AddDefaultTokenProviders();
}


public async Task<IActionResult> Create(UserCreateVM model)
    {
        if(ModelState.IsValid)
        {
            AppUser user = new AppUser
            {
                UserName = model.Name,
                Email = model.Email,
            };

            IdentityResult Result = await _userManager.CreateAsync(user);

            if(Result.Succeeded)
            {
                return RedirectToAction("Index");
            }

            else
            {
                foreach (var item in Result.Errors)
                {
                    ModelState.AddModelError("", item.Description);
                }
            }
        }

        else
        {
            return View(model);
        }
    }

1 个答案:

答案 0 :(得分:0)

我编辑此行:

IdentityResult Result = await _userManager.CreateAsync(user);

至:

IdentityResult Result = await _userManager.CreateAsync(user,model.Password);

问题已解决。