簡體   English   中英

ASP.NET 會話已過期或找不到

[英]ASP.NET session has expired or could not be found

我在 Windows Azure 上部署了一個 asp.net(VS2010 - .NET 4.0) 應用程序。 我正在嘗試使用 .NET 報告查看器控件。 我正在本地模式下運行報告。 報告工作正常並正確顯示。 當我嘗試將其導出到 Excel/PDF/Word 時,出現此錯誤“ASP.NET 會話已過期或無法找到”。

這是我的示例代碼:ASP.NET

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RxCloudReports.Default" EnableSessionState="True" %>

<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
</script>
</head>
</head>
<body>
    <form id="form1" runat="server" enctype="multipart/form-data">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>

        <rsweb:ReportViewer ID="ReportViewer1" runat="server" ProcessingMode="Local" SizeToReportContent="true" AsyncRendering = "true" 
                     ShowPageNavigationControls="true"   ShowExportControls="true" ShowToolBar="true" >

        </rsweb:ReportViewer>
</form>
</body>
</html>

C#

DataSet ds = GetDataSet(_ID, _Module);
ReportViewer1.Reset();
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = "Reports/Reports1.rdlc";
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("ReportDS", ds.Tables[0]));
ReportViewer1.LocalReport.Refresh();

我已經設定

<rsweb:ReportViewer ... KeepSessionAlive="false" />

在此之后,它在 Azure 上工作(至少對我而言)。

請檢查此鏈接。

這個鏈接對我有用。

ASP.NET 會話已過期或找不到 -> 因為 Session.SessionID 更改(報告服務)

謝謝。

就我而言; 位於 iframe 中的 ReportViewer 控件一切正常,但最近幾天我的網站停止工作,原因是 google chrome 安全更新; 解決方案為我的報表服務器設置了一個子域,即我的網站位於https://app.myapp.com我的報表服務器位於https://reports.myapp.com

注意:此解決方案僅適用於使用 iframe 進行報告控制並且站點位於不同域中的情況

參考: 開發人員:為新的 SameSite=None 做好准備; 安全 Cookie 設置公告:SameSite Cookie 處理和 Azure 應用服務上的 .NET Framework 4.7.2 補丁可用性

除了 Web 場、工作進程循環、會話實際過期等之外,還有另一種情況會發生“ASP.NET 會話已過期”錯誤。

這種情況非常嚴重,我沒有在 Stackoverflow 上看到針對它的答案,所以如果這發生在其他人身上,這有望減輕痛苦。

這種情況可能發生在任何使用嵌入式報表查看器控件的應用程序中,但如果您的應用程序具有多個頻繁刷新的嵌入式報表查看器控件,則它會更快地出現。

發生的情況是報表查看器控件每次刷新時都會不斷創建新的 cookie(不知道為什么)。 cookie 名稱的后綴為 _SKA。

由於每個瀏覽器對它們將存儲的 cookie 數量都有限制(對於最新的 IE 版本,它是 50,對於 Chrome 和 Firefox,它是一個更大的數字,但仍然有限),cookie 會累積,直到達到限制瀏覽器開始丟棄最舊的 cookie,這意味着 ASP.NET 的會話 cookie 最終會被丟棄,從而導致 ASP.NET 會話過期錯誤。

所以在這種情況下,它與會話過期或服務器端的任何事情無關。 當達到瀏覽器 cookie 計數限制時,客戶端瀏覽器開始丟棄最舊的 cookie。

我從這個問題中提出了這個解決方案,其中 cookie 限制問題導致了另一個問題。 下面的代碼基於該問題的解決方案: SSRS:為什么 SKA-cookies 建立直到:HTTP 400 Bad Request - Request Too Long

將此代碼添加到 global.asax 將解決問題:

    void Application_BeginRequest(object sender, EventArgs e) {
        if (Request == null || Request.Cookies == null) {
            return;
        }
        if (Request.Cookies.Count < 10) {
            return;
        }
        for (int i = 0; i < Request.Cookies.Count; ++i) {
            if (StringComparer.OrdinalIgnoreCase.Equals(Request.Cookies[i].Name, System.Web.Security.FormsAuthentication.FormsCookieName)) {
                continue;
            }
            if (!Request.Cookies[i].Name.EndsWith("_SKA", System.StringComparison.OrdinalIgnoreCase)) {
                continue;
            }
            if (i > 10) {
                break;
            }

            System.Web.HttpCookie c = new System.Web.HttpCookie(Request.Cookies[i].Name);
            c.Expires = DateTime.Now.AddDays(-1);
            c.Path = "/";
            c.Secure = false;
            c.HttpOnly = true;
            Response.Cookies.Set(c);
        }
    }

暫無
暫無

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

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