身份框架用户锁定
Identity Framework User Lockdown
我试图在 3 次登录尝试失败 5 分钟后锁定用户登录。我已将这 3 行添加到 App_Start/IdentityConfig.cs
public static ApplicationUserManager Create( ... )
方法:
manager.MaxFailedAccessAttemptsBeforeLockout = 3;
manager.DefaultAccountLockoutTimeSpan = new TimeSpan(0, 5, 0);
manager.UserLockoutEnabledByDefault = true;
之后,我通过 POST /api/Account/Register
注册了新用户(在默认脚手架 AccountController
中)。帐户已创建,LockoutEnabled
属性 设置为 true
。但是,如果我尝试通过 POST /Token
登录几次但密码错误,则帐户未被锁定。
我也对 /Token
端点的实现位置感兴趣。是否在AccountController
GET api/Account/ExternalLogin
。我在那里设置了断点,但是当我尝试登录时,执行并没有在那里停止。
我错过了什么?
如果您使用 Visual Studio 中的默认 Web API 模板,则必须更改 ApplicationOAuthProvider
class 的 GrantResourceOwnerCredentials
方法的行为(在 Web API 项目的 Provider 文件夹中找到)。像这样的东西可以让你跟踪失败的登录尝试,并阻止被锁定的用户登录:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
var user = await userManager.FindByNameAsync(context.UserName);
if (user == null)
{
context.SetError("invalid_grant", "Wrong username or password."); //user not found
return;
}
if (await userManager.IsLockedOutAsync(user.Id))
{
context.SetError("locked_out", "User is locked out");
return;
}
var check = await userManager.CheckPasswordAsync(user, context.Password);
if (!check)
{
await userManager.AccessFailedAsync(user.Id);
context.SetError("invalid_grant", "Wrong username or password."); //wrong password
return;
}
await userManager.ResetAccessFailedCountAsync(user.Id);
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
OAuthDefaults.AuthenticationType);
ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties properties = CreateProperties(user.UserName);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
请注意,通过这种方式您只能锁定尝试使用 password
授权(资源所有者凭据)登录的用户。如果您还想禁止被锁定的用户使用其他授权登录,则必须覆盖其他方法(GrantAuthorizationCode
、GrantRefreshToken
等),检查这些方法中的 await userManager.IsLockedOutAsync(user.Id)
是否为真方法也是。
我试图在 3 次登录尝试失败 5 分钟后锁定用户登录。我已将这 3 行添加到 App_Start/IdentityConfig.cs
public static ApplicationUserManager Create( ... )
方法:
manager.MaxFailedAccessAttemptsBeforeLockout = 3;
manager.DefaultAccountLockoutTimeSpan = new TimeSpan(0, 5, 0);
manager.UserLockoutEnabledByDefault = true;
之后,我通过 POST /api/Account/Register
注册了新用户(在默认脚手架 AccountController
中)。帐户已创建,LockoutEnabled
属性 设置为 true
。但是,如果我尝试通过 POST /Token
登录几次但密码错误,则帐户未被锁定。
我也对 /Token
端点的实现位置感兴趣。是否在AccountController
GET api/Account/ExternalLogin
。我在那里设置了断点,但是当我尝试登录时,执行并没有在那里停止。
我错过了什么?
如果您使用 Visual Studio 中的默认 Web API 模板,则必须更改 ApplicationOAuthProvider
class 的 GrantResourceOwnerCredentials
方法的行为(在 Web API 项目的 Provider 文件夹中找到)。像这样的东西可以让你跟踪失败的登录尝试,并阻止被锁定的用户登录:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
var user = await userManager.FindByNameAsync(context.UserName);
if (user == null)
{
context.SetError("invalid_grant", "Wrong username or password."); //user not found
return;
}
if (await userManager.IsLockedOutAsync(user.Id))
{
context.SetError("locked_out", "User is locked out");
return;
}
var check = await userManager.CheckPasswordAsync(user, context.Password);
if (!check)
{
await userManager.AccessFailedAsync(user.Id);
context.SetError("invalid_grant", "Wrong username or password."); //wrong password
return;
}
await userManager.ResetAccessFailedCountAsync(user.Id);
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
OAuthDefaults.AuthenticationType);
ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties properties = CreateProperties(user.UserName);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
请注意,通过这种方式您只能锁定尝试使用 password
授权(资源所有者凭据)登录的用户。如果您还想禁止被锁定的用户使用其他授权登录,则必须覆盖其他方法(GrantAuthorizationCode
、GrantRefreshToken
等),检查这些方法中的 await userManager.IsLockedOutAsync(user.Id)
是否为真方法也是。