Session expires after file download

非 Y 不嫁゛ 提交于 2019-12-11 07:58:20

问题


When a user downloads a file from my ASP.NET application, the session expires a few seconds after they download the file. Before the session expires that can perform any task, but after about 5-10 seconds, the session is restarted and they get logged out.

I've created a simple page to demonstrate this. To run this simple page, create a new asp.net c# project, then insert the code into a new page.

EDIT: This appears to be a IE7 problem. Firefox and Chrome are unaffected.

I believe the code responsible for the session restart is:

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();

To recreate this problem:

  1. Copy the code below into an asp.net page.
  2. Use IE (I used IE7, firefox and chrome don't appear to have this issue)
  3. Notice that the session is new.
  4. Refresh the page; notice that the session is not new.
  5. Download the file and save it.
  6. Hit the "Refresh Page" button a couple of times until the "Session is new" text is redisplayed (about 10 seconds).

Below is the code for the simple recreation:

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

回答1:


Something to do with this (could be a similar issue)? Maybe use Fiddler to see what is happening in more detail to the cookie.




回答2:


Make sure you start with a FRESH BROWSER after getting rid of the Response.Clear() call. I had the same exact problem, read this post, and getting rid of the Response.Clear() worked, but ONLY after I dumped the browser that was sitting open on my desktop, and opened up a fresh instance. good luck!




回答3:


Hm. Could it be that Response.Clear() eats the session state cookie?



来源:https://stackoverflow.com/questions/1370285/session-expires-after-file-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!