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

时间:2018-08-02 19:09:55

标签: c# asp.net session-variables session-cookies

在添加以下代码之前,我的代码还可以。现在,当我提交表单后单击首页时,我将重定向到登录页面。

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

第一段:

        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;
            }

1 个答案:

答案 0 :(得分:0)

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

如果可能的话,最简单的解决方案是不写到该文件夹​​,如果绝对需要,那么您将不得不从内存会话管理转移到另一种方法

相关问题