簡體   English   中英

VaryByCustom不適用於會話變量

[英]VaryByCustom not working for session variable

我正在使用輸出緩存來登錄帶有登錄系統的網站。 我有全局頁面,每個用戶都可以訪問。 這些頁面被緩存並使用母版頁。

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

我在會話中存儲用戶登錄詳細信息。 我的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;
}

我在母版頁面中有一個面板,對於經過身份驗證的用戶可見。 當用戶登錄並查看公共頁面時,另一個訪客用戶也會在頁面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;
}

我認為由於某種原因,Session [“UserID”]可能總是返回null /或有時返回null,即使用戶已經過身份驗證。

在此功能要求之前仔細檢查您是否設置了它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM