Security 使用标识在.net核心应用程序中强制使用2FA
我已经在.net核心mvc应用程序中使用Security 使用标识在.net核心应用程序中强制使用2FA,security,model-view-controller,asp.net-core-2.0,asp.net-core-identity,Security,Model View Controller,Asp.net Core 2.0,Asp.net Core Identity,我已经在.net核心mvc应用程序中使用 这一切工作正常,但它依赖于用户进入他们的帐户并设置2FA。我有没有办法强迫用户这么做,这样所有用户都必须使用2FA 一种方法是在登录过程中,您通过以下方式检查用户是否设置了2FA: var user = await _userManager.FindByEmailAsync(Input.Email); var twoFactorEnabled = user.TwoFactorEnabled; 如果为false,则可以重定向用户以配置2FA页面(/Man
这一切工作正常,但它依赖于用户进入他们的帐户并设置2FA。我有没有办法强迫用户这么做,这样所有用户都必须使用2FA 一种方法是在登录过程中,您通过以下方式检查用户是否设置了2FA:
var user = await _userManager.FindByEmailAsync(Input.Email);
var twoFactorEnabled = user.TwoFactorEnabled;
如果为false,则可以重定向用户以配置2FA页面(
/Manage/TwoFactorAuthentication
),在用户设置2FA并启用2FA后,AspNetUsers
表中的TwoFactorEnabled
将为True
,然后在登录过程中,如果当前用户的twofacturenabled
值为true,identity将自动将用户重定向到/LoginWith2fa
页面进行2FA登录。我有点晚了。但实现这一点的方法是创建一个自定义控制器并重写OnActionExecuting方法
public class CustomController : Controller
{
public readonly DataContext _db;
public CustomController(DataContext context)
{
_db = context;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (User.Identity.IsAuthenticated)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
UserModel user = _db.UserModel.Find(userId);
if (user.TwoFactorEnabled==false && ControllerContext.ActionDescriptor.ActionName != "TwoFactor")
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Home", action = "TwoFactor" }));
return;
}
}
base.OnActionExecuting(filterContext);
}
}
这个实现的问题是,用户可以通过url导航到别处来避免这种情况,因此使2FA不能真正实施。这看起来是一个很好的解决方案