【问题标题】:Session expires after file download文件下载后会话过期
【发布时间】:2009-09-02 21:39:49
【问题描述】:

当用户从我的 ASP.NET 应用程序下载文件时,会话会在他们下载文件几秒钟后过期。在可以执行任何任务的会话到期之前,但在大约 5-10 秒后,会话会重新启动并且它们会被注销。

我创建了一个简单的页面来演示这一点。要运行这个简单的页面,请创建一个新的 asp.net c# 项目,然后将代码插入一个新页面。

编辑:这似乎是一个 IE7 问题。 Firefox 和 Chrome 不受影响。

我相信负责会话重启的代码是:

HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=test.xml");
HttpContext.Current.Response.Write("<test>this is a test.</test>");
HttpContext.Current.Response.End();

重现这个问题:

  1. 将下面的代码复制到一个asp.net页面中。
  2. 使用 IE(我用的是 IE7、firefox 和 chrome 似乎没有这个问题)
  3. 注意会话是新的。
  4. 刷新页面;请注意会话不是新的。
  5. 下载文件并保存。
  6. 点击“刷新页面”按钮几次,直到重新显示“会话是新的”文本(大约 10 秒)。

下面是简单娱乐的代码:

<%@ Page Language="C#" AutoEventWireup="true" %>
<!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>Untitled Page</title>
</head>
<body>
    <script runat="server">
        private string sessionString {
            get {
                return HttpContext.Current.Session["sessionString"] == null ? null : HttpContext.Current.Session["sessionString"].ToString();
            }
            set {
                HttpContext.Current.Session["sessionString"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e) {
            Label1.Text = sessionString ?? "Session is null";
            if(sessionString == null) {
                Label1.Text = "Session is new";
                Label1.BackColor = System.Drawing.Color.Red;
                sessionString = "Session is now not null";
            }
            else {
                Label1.Text = sessionString;
                Label1.BackColor = System.Drawing.Color.White;
            }
        }
        protected void LinkButton1_Click(object sender, EventArgs e) { }
        protected void LinkButton2_Click(object sender, EventArgs e) {
            HttpContext.Current.Response.ContentType = "text/xml";
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=test.xml");
            HttpContext.Current.Response.Write("<test>this is a test.</test>");
            HttpContext.Current.Response.End();
        } 
    </script>
    <form id="form1" runat="server">
    <div>
        <asp:Label id="Label1" runat="server" text="Label"></asp:Label> <br />
        <asp:LinkButton id="LinkButton1" runat="server" onclick="LinkButton1_Click">Refresh Page</asp:LinkButton> <br />
        <asp:LinkButton id="LinkButton2" runat="server" onclick="LinkButton2_Click">Download File</asp:LinkButton> <br /><br />
        <b>Steps to recreate:</b>
        <ol>
            <li>Download the file and save it.</li>
            <li>Hit the "Refresh Page" button a couple of times until the "Session is new" text is redisplayed.</li>
            <li>Answer my question explaining what the heck is going on!</li>
        </ol>
    </div>
    </form>
</body>
</html>

【问题讨论】:

    标签: c# asp.net session session-timeout


    【解决方案1】:

    Something to do with this(可能是类似的问题)?也许使用 Fiddler 来更详细地查看 cookie 发生了什么。

    【讨论】:

    • 即使不是,也应该是!
    • 我不知道是不是这样,但是用最新的更新重新安装 ie7 后,它工作正常。谢谢。
    • @Chris:我也有同样的问题。但对我来说,它也出现在 chrome 和 firefox 中.. 没有签入 ie.. 我打开了管理端,它的登录是由 firefox 中的会话管理的。它有一个客户端,下载发生在 chrome 中打开。下载后,当我检查会话以获取我的登录详细信息时,它似乎为空。你能告诉它是如何修复的。只安装即更新。还有其他方法吗?
    • @mehesh,就像我在上一条评论中所说的那样。对我来说,当我重新安装 ie7 时它已修复。听起来您的症状相似,但问题不同。
    【解决方案2】:

    请确保在摆脱 Response.Clear() 调用后从 FRESH BROWSER 开始。我遇到了同样的问题,阅读了这篇文章,并且摆脱了 Response.Clear() 工作,但只有在我转储了在我的桌面上打开的浏览器并打开了一个新实例之后。祝你好运!

    【讨论】:

      【解决方案3】:

      嗯。难道Response.Clear()吃掉了会话状态cookie?

      【讨论】:

      • 这似乎不是问题。如果你注释掉我给出的示例中的代码,你会遇到同样的问题。
      猜你喜欢
      • 2013-02-06
      • 1970-01-01
      • 2019-09-21
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 2012-04-29
      • 2018-12-05
      相关资源
      最近更新 更多