简体   繁体   English

在ASP.NET MVC中使用身份验证的OutputCache困境

[英]OutputCache dilemma with authentication in ASP.NET MVC

I'm writing an ASP.NET MVC site that includes the possibility for users to create accounts and log in. As I also want to cache the site aggressively, I'm running into some trouble mixing caching with authentication. 我正在编写一个ASP.NET MVC站点,其中包括用户创建帐户和登录的可能性。由于我还想积极地缓存该站点, 我在将缓存与身份验证混合时遇到了一些麻烦。

On every page, at the top, if the user is logged in, I output their username, a link to their profile, and a link to log out. 在每个页面的顶部,如果用户已登录,我会输出其用户名,指向其个人资料的链接以及注销链接。 If they're not logged in, I output a standard login link. 如果他们没有登录,我输出一个标准的登录链接。 Plus, in the page itself, some content is not shown to unauthenticated users, while other content depends on which user is logged in. 此外,在页面本身中,某些内容不会显示给未经身份验证的用户,而其他内容则取决于登录的用户。

I first attempted to solve this problem some time ago by asking the Stack Overflow team how they solved the problem. 我前一次尝试通过询问Stack Overflow团队如何解决问题来解决这个问题。 Jeff replied that they basically do no caching at all for unauthenticated users. 杰夫回答说,对于未经身份验证的用户,他们基本上根本没有缓存。 So, I wrote an attribute that derives from OutputCacheAttribute but cancels caching if the user is logged in . 因此,我编写了一个派生自OutputCacheAttribute的属性,但如果用户已登录则取消缓存

Currently, I'm using that attribute, but I'm getting incorrect results in some cases. 目前,我正在使用该属性,但在某些情况下我得到的结果不正确 For example, the user can visit some page, then log in, then visit the page again, only to see the login link at the top, rather than their username. 例如,用户可以访问某个页面,然后登录,然后再次访问该页面,只能看到顶部的登录链接,而不是他们的用户名。

Here are some solutions that I'm considering: 以下是我正在考虑的一些解决方案:

  • Setting the HttpCacheability or Cache-Control type to private , rather than public . HttpCacheabilityCache-Control类型设置为private ,而不是public This way, the response is only cached client-side. 这样,响应仅缓存在客户端。 Will this fix the problem? 这会解决这个问题吗? If it does, will this have an effect on the efficiency of caching? 如果是这样,这会对缓存效率产生影响吗? I've noticed that Stack Overflow seems to use public , however. 我注意到Stack Overflow似乎使用public
  • Setting up a VaryByCustom parameter to cache differently for each user , like in this tutorial . 设置VaryByCustom参数以针对每个用户进行不同的缓存 ,如本教程中所述 Will this help, while still maintaining the efficiency and effectiveness of caching? 这会有帮助吗,同时仍然保持缓存的效率和效果?

Thanks in advance! 提前致谢!

Depending on the structure of your application, it may make sense to cache data rather than views. 根据应用程序的结构,缓存数据而不是视图可能有意义。

Since views are very simple and database access usually takes most of the time required to render a page, you can get most of the benefit of output caching by caching the model in your controller, and any uncachable parts of the view will be unaffected so you can cache public content being viewed by authenticated users. 由于视图非常简单,并且数据库访问通常占用呈现页面所需的大部分时间,因此您可以通过在控制器中缓存模型来获得输出缓存的大部分好处,并且视图的任何无法访问的部分都不会受到影响,因此您可以缓存经过身份验证的用户正在查看的公共内容

There are also ways to make output caching work with partial views, but in my opinion they add more complexity than is really justified. 还有一些方法可以使输出缓存与部分视图一起工作,但在我看来,它们增加了比真正合理的复杂性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM