VaryByCustom不适用于会话变量

Has*_*anG 7 c# asp.net caching outputcache varybyparam

我正在使用输出缓存来登录带有登录系统的网站.我有全局页面,每个用户都可以访问.这些页面被缓存并使用母版页.

<%@ OutputCache Duration="3600" VaryByParam="none" VaryByCustom="userid" %>
Run Code Online (Sandbox Code Playgroud)

我在会话中存储用户登录详细信息.我的global.asax文件在这里:

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;
}
Run Code Online (Sandbox Code Playgroud)

我在母版页面中有一个面板,对于经过身份验证的用户可见.当用户登录并查看公共页面时,另一个访客用户也会在页面A上看到经过身份验证的用户面板.如果访客首先查看页面A,则经过身份验证的用户不会在页面A上看到面板.

我的代码的哪一部分是错的?我第一次使用VaryByCustom.

编辑

我已经像这样修改了我的global.asax,但是文本文件中没有写入:

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;
}
Run Code Online (Sandbox Code Playgroud)

Ari*_*tos 0

我认为 Session["UserID"] 出于某种原因可能总是返回 null / 或者有时返回 null,即使用户已通过身份验证。

在此函数请求之前,请仔细检查您是否已设置它。

  • @HasanGursoy 所以我对会议有权利!尝试获取会话的 cookie。与您尝试的想法相同。 (2认同)