I have an ASP.NET MVC application. I need to cache some actions (pages) however for some pages I need do such only for non authenticated users.
I've tried to user VaryByCustom="user"
with following GetVaryByCustomString
implementation.
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == "user")
{
if (context.User.Identity.IsAuthenticated)
{
return context.User.Identity.Name;
}
else
{
return "";
}
}
return base.GetVaryByCustomString(context, custom);
}
However this isn't exactly what I need because pages are still cached. Only difference is that now is cached for each user separately.
One possible solution is to return Guid.NewGuid()
each time when user is Authenticated, but I looks like huge waste of resources to me.
So do you have any tips for me?