Вы можете воспользоваться преимуществом VaryByCustom
свойства в [OutputCache]
, переопределив HttpApplication.GetVaryByCustomString
и отметив HttpContext.Current.User.IsAuthenticated.
. Это то, что я создам в файле Global.asax.cs:
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == "UserName")
{
if (context.Request.IsAuthenticated)
{
return context.User.Identity.Name;
}
return null;
}
return base.GetVaryByCustomString(context, custom);
}
А затем используйте его в атрибуте OutputCache:
[OutputCache(Duration = 10, VaryByParam = "none", VaryByCustom = "UserName")]
public ActionResult Profiles()
{
//...
}
, но будьте осторожны, чтобы в этом случае имя пользователя было неизменным!