8

I'm using output cache for a web site with login system. I have global pages which can be accessed by every user. These pages are cached and also use a master page.

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

I'm storing user login details 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 in master page which is visible for authenticated users. When a user logins and views public page A another guest user also sees authenticated user panel on page A. If guest first view page A then authenticated user does not see panel on page A.

What part of my code is wrong? I'm using VaryByCustom for first time.

EDIT

I've modified my global.asax like this but nothing is written in 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;
}
HasanG
  • 12,734
  • 29
  • 100
  • 154
  • OK, I've found that I cannot access current session variable "Session state is not available in this context.". Trying to fix it now. – HasanG Nov 16 '10 at 09:56

1 Answers1

0

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

Double check that you set it before this function ask for it.

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • I see that the session variable is changed but problem is GetVaryByCustomString is not fired/work properly I think – HasanG Nov 16 '10 at 09:39
  • @hasanGursoy Yes somewhere there is the problem, but if the Function is called, then the session parameter is not set. – Aristos Nov 16 '10 at 10:36
  • Tried everything but it seems **Session** is not available when it's requested by cached page varybycustom, it only works on user controls. – HasanG Nov 16 '10 at 13:44
  • 2
    @HasanGursoy So I have right about the Session !. Try just to get the cookie of the session. Is the same think as the one you try. – Aristos Nov 16 '10 at 15:57
  • I can't use cookies for user authentication. So I've disabled caching at all pages. – HasanG Nov 16 '10 at 16:30