VaryByCustom not working for session variable

I am using output cache for website with login system. I have global pages that every user can access. These pages are cached and also use the main page.

<%@ OutputCache Duration="3600" VaryByParam="none" VaryByCustom="userid" %> 

I store user login data in a session. My global.asax file is here:

 public override string GetVaryByCustomString(HttpContext context, string arg) { string result = String.Empty; if (arg == "userid") { object o = Session["UserID"]; if (o != null) { result = o.ToString(); } } else { result = base.GetVaryByCustomString(context, arg); } return result; } 

I have a panel on the main page that is visible to authenticated users. When a user logs in and views a public page, another guest user also sees the authenticated user panel on page A. If the first guest viewing page is A, then the authenticated user does not see the panel on page A.

What part of my code is incorrect? I am using VaryByCustom for the first time.

EDIT

I changed my global.asax file but nothing is written to the text file:

 public override string GetVaryByCustomString(HttpContext context, string arg) { string result = String.Empty; FileInfo t = new FileInfo(Server.MapPath("App_Data\\debug.txt")); StreamWriter Tex = t.AppendText(); Tex.WriteLine("GetVaryByCustomString: " + arg); if (arg == "userid") { object o = Session["UserID"]; if (o != null) { result = o.ToString(); } Tex.WriteLine("Checked UserID: " + o + Tex.NewLine); } else { result = base.GetVaryByCustomString(context, arg); } Tex.Close(); return result; } 
+7
c # caching outputcache
source share
1 answer

I think that probably Session ["UserID"] for some reason always returns null / or returns null several times, even if the user is authenticated.

Double check that you installed it before this function asks for it.

0
source share

All Articles