在服务器上缓存我的操作方法,将允许未经授权的用户访问相关的操作方法

时间:2014-01-02 14:50:38

标签: asp.net-mvc asp.net-mvc-4 caching

我有以下操作方法,在服务器设置上有缓存:

[CheckUserPermissions(Action = "", Model = "Admin")]
[OutputCache(CacheProfile = "short", Location = OutputCacheLocation.Server, VaryByHeader = "X-Requested-With")]
public ActionResult SystemInfo(int page = 1,bool forTechAudit=false)
{ 

CheckUserPermision操作过滤器会将未经授权的消息返回给没有所需权限的用户,如下所示:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class CheckUserPermissionsAttribute : ActionFilterAttribute
    {
//code goes here
// IF user is not authorized then ….
var viewResult = new ViewResult();
viewResult.ViewName = "~/Views/Errors/_Unauthorized.cshtml";
filterContext.Result = viewResult; }}

base.OnActionExecuting(filterContext);}}}

现在,如果用户访问SystemInfo操作方法而他未获得授权,则会获得/_Unauthorized.cshtml视图,但如果其他用户获得授权并调用SystemInfo操作方法,他将会也可以获得*/_Unauthorized.cshtml*视图。

此外,如果授权用户首先访问SystemInfo,那么未经授权的用户将能够看到调用操作方法并查看缓存结果?甚至当前的登录用户名都将被缓存,用户将在其会话中看到其他用户名。

有人可以建议我如何克服这个问题吗?目前我将缓存位置更改为客户端而不是临时解决问题的服务器,但我需要在服务器上缓存操作方法。我正在考虑将loginusername传递给我的操作方法 - 任何建议?

由于

1 个答案:

答案 0 :(得分:0)

请参阅以下问题:

我仔细阅读了这些问题,OutputCacheAttribute似乎与AuthorizeAttribute配合得很好。所以也许你应该检查AuthorizeAttribute如何处理缓存,并在你的actionfilter中实现它。

我个人喜欢这种方法:https://meta.stackexchange.com/questions/60403/how-does-stack-overflow-do-caching/60406#60406


修改:查看AuthorizeAttribute here的来源,然后查看OnAuthorization功能。也许有帮助。