Page cache issues that need to be noted when customizing AuthorizeAttribute in ASP. net mvc 3

Source: Internet
Author: User
I. Using OutputCache in ASP. NET MVC to implement server-side page-level cache

In ASP. net mvc, if we want to cache a page (that is, an Action) on the server side, we can mark the following features on the Action:

1 [OutputCache(Duration = 10, VaryByParam = "*", Location = OutputCacheLocation.Server)] 2 public ActionResult Login() 3 { 4     ViewBag.Message = "Your app description page. " + DateTime.Now.ToString(); 5     LoginModel loginModel = new LoginModel(); 6     ViewBag.IsValCodeVisible = IsValCodeVisible(); 7     return View(new LoginModel()); 8 } 

To enable the OutputCache feature to take effect, the following conditions are required:

1. The Action must be [HttpGet].

2. Set <system. Web>/<compilation debug = "false"> in web. Config, that is, the compiling condition of the application cannot be Debug.

3. Cookies are not contained in the Response.

  • 1) before. NET Framework 2.0.50727.4209, the page response containing Cookies can output the cache. The source code is as follows:

 

  • 2) from. NET Framework 2.0.50727.4209, Cookies are added. If the page response contains Cookies, the code is re-executed instead of being cached.

ASP. net mvc 4 is Based on. NET Framework 4 or 4.5. Therefore, Cookies are also judged. This means that if the Cookie output mode for Forms authentication is HttpOnly,

The verified page will not output the page cache, because each response will contain the Cookie generated after the authentication ticket is encrypted.

Ii. Why should I try not to rewrite the OnAuthorization () method of AuthorizeAttribute?

The OnAuthorization () method in the source code of AuthorizeAttribute has the following annotation:

// Since we're performing authorization at the action level, the //authorization code runs // after the output caching module. In the worst case this could allow //an authorized user // to cause the page to be cached, then an unauthorized user would //later be served the // cached page. We work around this by telling proxies not to cache the //sensitive page, // then we hook our custom authorization code into the caching //mechanism so that we have // the final say on whether a page should be served from the cache. 

This method handles the server-side page cache problem, ensuring that each unverified user needs to be verified, rather than directly reading the server-side page cache. In what scenarios will the cache problem occur and what is the specific performance of the cache problem? The following is a demonstration.

1. Create an ASP. net mvc 4 Web application, and select "Internet application" as "Project template ".

2. Define a class that inherits to "authorizeatrriization". This class overrides OnAuthorization () and does not process OutputCache.

 1 public class CustomAuthorizeAttribute : AuthorizeAttribute  2 {  3     public override void OnAuthorization(AuthorizationContext filterContext)  4     {  5         if (!filterContext.HttpContext.User.Identity.IsAuthenticated)  6         {  7             HandleUnauthorizedRequest(filterContext);  8         }  9     } 10 } 

 

3. Locate Controllers/HomeController/Index and mark the following features.

1 [HttpGet] 2 [CustomAuthorize] 3 [OutputCache (Duration = 30, Location = OutputCacheLocation. server, VaryByParam = "*")] 4 public ActionResult Index () 5 {6 ViewBag. message = "modify this template to quickly start your ASP.. net mvc application. "; 7 return View (); 8}

 

4. After the preceding three steps, the Home/Index page will be cached on the server after a user accesses the Home/Index as expected (Location = OutputCacheLocation. in the future, when another user accesses Home/Index, the system will directly read the page cache, you do not need to re-execute the Home/Index Code (including the code for features such as identity authentication ). To simulate two different users, select Chrome and IE to access the page.

First open the Chrome browser, access Account/Login, enter the Account password to log on, and then visit Home/Index on the Home page, as shown below:

 

Then open IE and access Home/Index directly. If there is no page cache, you cannot open the page directly at this time, because the Home/Index has already marked [CustomAuthorize], which requires authentication, but because of the page cache, therefore, the server directly sends the request to IE from the cache left by the server when Chrome accesses Home/Index for the first time. The page is displayed as follows:

 

By default, ASP. NET saves cookies instead of HttpOnly and does not include cookies in each page response. The OutputCache on the server can take effect. In this case, rewriting the onauthorizeattribute () will cause a problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.