Search code examples
asp.netasp.net-mvchttp-caching

Caching anonymous page with redirect if user is authenticated


I would like to use page caching for our asp.net mvc site's landing page. This page is accessible to anonymous users only. If the user is logged in however, I would like this page to redirect to the user home page which is authenticated.

The problem is that if I cache the landing page, a logged in user can still access this page because it is cached on their browser.

Obviously a meta refresh header if the user is authenticated will also not work because the page will cache without the header in the first place.

Is there any way to cache the landing page but also redirect if the user is logged in, without using query params, or is conditional GET the only way to do this?


Solution

  • You could use the OutputCacheAttribute, with the VaryByCustom parameter.

    If you set it to something like "IsAuthenticated" and then define the GetVaryByCustomString method in your Global.asax file, and return a string of "True" or "False" depending on if the user is authenticated or not perhaps.

    In fact, the following Blog Post answers your exact question: Read This