Here is how I implemented above.
In Global.asax.cs:
public override string GetVaryByCustomString(HttpContext context, string custom) { if (custom == "UserId") { if (context.Request.IsAuthenticated) { return context.User.Identity.Name; } return null; } return base.GetVaryByCustomString(context, custom); }
Using the output cache attribute:
[OutputCache(Duration = 30, VaryByCustom = "UserId" ... public ActionResult MyController() { ...
source share