我有根据用户 ID 和用户角色返回真或假的函数。
我有几个动作结果的控制器。
例如
public class DemoController : Controller
{
public ActionResult Index(){}
public ActionResult Contact(){}
}
所以我想,每次当用户使用这个动作时,检查用户是否在角色中。
我知道我可以做到
[Authorize(Roles = "Administrator")]
public ActionResult JustAdmins(){}
但是这种方式,每次用户访问这个动作,都是一个额外的 SQL 查询。
我想在 MemCached 中存储用户角色,所以我的功能就像
public static bool IsCompany(Guid UserID)
{
//if (get from cache != null && get from cache == "Role")
// return true
//if (get from DB != null && get from DB == "Role")
//return true
return false;
}
但是我怎样才能继承控制器中的所有动作来检查这个功能呢?
提示:也许覆盖 OnActionExecuting 或类似的?