这实际上取决于您的身份验证管道的工作方式,这是一个一般的想法:每个请求都从数据库中获取用户的数据:
public User CurrentUser
{
get
{
if (_CurrentUser == null && Request.IsAuthenticated)
{
//your method to get the user
_CurrentUser = _UserRepository.GetUserByEmail(HttpContext.User.Identity.Name);
}
return _CurrentUser;
}
}
然后使用 ActionFilter 您可以检查:
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
//user enabled?
if (CurrentUser != null && !CurrentUser.IsApproved)
{
//Force LogOut
//Redirect to LogOut
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary {{ "Controller", "Account" },
{ "Action", "LogOff" } });
}
}