1

当用户从我的 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>
4

3 回答 3

2

与此有关(可能是类似的问题)?也许使用 Fiddler 来更详细地查看 cookie 发生了什么。

于 2009-09-03T15:26:36.317 回答
1

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

于 2011-07-13T16:16:36.690 回答
0

嗯。会不会是Response.Clear()吃会话状态cookie?

于 2009-09-02T22:21:45.737 回答