ASP.NET 中丢失会话数据。重定向到登录页面

Losing session data in ASP.NET. Redirected to Login page

在添加以下代码之前,我的代码一切正常。现在,当我在提交表单后单击主页时,我将被重定向到登录页面。

我在不同的部分添加了以下两个代码块,它们都使我的会话变量超时

第一块:

        DateTime date1 = DateTime.UtcNow;
        string dateTime = date1.ToString("yyyyMMddHHmm");
        string fileName = string.Concat(string.Concat(jRequestId, "-"), dateTime);
        string jsonLogFolderPath = "~/App_code/jsonLog";
        if (!System.IO.Directory.Exists(Server.MapPath(jsonLogFolderPath)))
        {
        System.IO.Directory.CreateDirectory(Server.MapPath(jsonLogFolderPath));
        }
        string path = Server.MapPath(jsonLogFolderPath + "/" + fileName + ".txt");

        if (!File.Exists(path))
        {
            using (System.IO.StreamWriter jsonLogFile = new System.IO.StreamWriter(path))
            {
                jsonLogFile.Write(json);
                jsonLogFile.Close();
            }
        }
        else
        {
            divErrorMsg.Style.Add("display", "block");
            ErrorLog.LogError(new Exception("Unable to create Text file"));
            dbconn.Close();
            return;
        }

第二块:`

            string requestID = Session["requestID"].ToString();

            string reportsFolderPath = "~/App_code/Reports";
            if (!System.IO.Directory.Exists(Server.MapPath(reportsFolderPath)))
            {
               System.IO.Directory.CreateDirectory(Server.MapPath(reportsFolderPath));
            }
            string reportsPath = Server.MapPath(reportsFolderPath + "/" + "ReportSubmissionSummary-" + requestID + ".pdf");

            if (!File.Exists(reportsPath))
            {
               BinaryWriter reportWriter = new BinaryWriter(new FileStream(reportsPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
              reportWriter.Write(bytes);
                reportWriter.Close();
            }
            else
            {
                divErrorMsg.Style.Add("display", "block");
                ErrorLog.LogError(new Exception("Unable to store the pdf Report"));
                dbconn.Close();
                return;
            }

更改 app_code 文件夹(或 bin 文件夹、web.config 文件、machine.config 文件)中的文件将导致应用程序重新启动,这将是您丢失会话的原因信息 - 假设您正在使用标准的 inproc 会话管理。

如果可能,最简单的解决方案是不写入该文件夹,如果您绝对必须这样做,那么您将不得不从内存会话管理转移到替代方法