asp.net 创建用户时,identity lockoutEnabled 始终设置为 true
asp.net identity lockoutEnabled is always set to true when creating a user
我编写了一个代码,使用 _userManager.CreateAsync
将用户添加到我的数据库,这是我的控制器代码:
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。是否有我需要更改的默认行为,或者我是否遗漏了什么?
默认设置为true
,您可以更改它ApplicationUserManeger
(默认ASP.NET身份代码)
public class ApplicationUserManager : UserManager<ApplicationUser, long>
{
// more code...
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
// more code...
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true; // <-- change this
// more code...
}
}
您可以在为您的应用程序配置安全设置时禁用它:
就我而言,在 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
,它应该可以解决问题。
我编写了一个代码,使用 _userManager.CreateAsync
将用户添加到我的数据库,这是我的控制器代码:
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。是否有我需要更改的默认行为,或者我是否遗漏了什么?
默认设置为true
,您可以更改它ApplicationUserManeger
(默认ASP.NET身份代码)
public class ApplicationUserManager : UserManager<ApplicationUser, long>
{
// more code...
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
// more code...
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true; // <-- change this
// more code...
}
}
您可以在为您的应用程序配置安全设置时禁用它:
就我而言,在 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
,它应该可以解决问题。