我编写了一段代码,使用_userManager.CreateAsync
将用户添加到我的数据库中,这是我的 Controller 代码:
public async Task<IActionResult> Create([FromForm] UserDetailsViewModel userview)
{
if (ModelState.IsValid)
{
SiteUser user = new SiteUser();
_mapper.Map(userview, user);//properties mapped using automapper.(works fine)
if (!userview.LockoutEnabled)
{
user.ExpirationTime = DateTime.MaxValue;//some custom property I added to my class
}
var result = await _userManager.CreateAsync(user,userview.Password);//user.LockoutEnabled is **false** here.
if (result.Succeeded)//user.LockoutEnabled is **true** here and also in database.
{
return new StatusCodeResult(StatusCodes.Status201Created);
}
else
{
return new StatusCodeResult(StatusCodes.Status400BadRequest);
}
}
else
{
return new StatusCodeResult(StatusCodes.Status400BadRequest);
}
}
这段代码工作正常并成功创建了用户。但是,当创建用户时,我的数据库中的 LockoutEnabled
字段等于 1。这不是我想要的。我在 CreateAsync(user,userview.Password) 之前设置了一个断点,并且用户将其锁定属性设置为 false。是否有我需要更改的默认行为,或者我是否遗漏了某些内容?
最佳答案
您可以在为应用程序配置安全设置时禁用它:
就我而言,在 Startup.cs
的 ConfigureServices
方法中 - 查看标记行:
var identityOptions = new Action<IdentityOptions>(options =>
{
options.SignIn.RequireConfirmedAccount = false;
options.Password.RequireDigit = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
options.Password.RequiredLength = 3;
// lockout setup
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(2);
options.Lockout.MaxFailedAccessAttempts = 2;
options.Lockout.AllowedForNewUsers = true; // <<-- This line controls it
});
services.AddDefaultIdentity<ApplicationUser>(identityOptions);
// Rest of identity configuration...
将 options.Lockout.AllowedForNewUsers
设置为 false
就可以了。
关于asp.net-mvc - 创建用户时 asp.net 身份 lockoutEnabled 始终设置为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68953907/