会话在文件下载后到期

时间:2009-09-02 21:39:49

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

当用户从我的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秒钟)。
  7. 以下是简单娱乐的代码:

    <%@ 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>
    

3 个答案:

答案 0 :(得分:2)

Something to do with this(可能是类似的问题)?也许使用Fiddler来更详细地了解cookie的内容。

答案 1 :(得分:1)

确保在删除Response.Clear()调用后开始使用新鲜浏览器。我有同样的问题,阅读这篇文章,并摆脱Response.Clear()工作,但只有在我倾倒了我的桌面上打开的浏览器,并打开了一个新的实例。祝你好运!

答案 2 :(得分:0)

嗯。可能是Response.Clear()吃会话状态cookie吗?