views:

322

answers:

2

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"&gt;
<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>
A: 

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

Tor Haugen
That doesn't appear to be the issue. If you comment out the code in the sample I gave, you get the same problem.
Chris
...with or without milk?
Peter Lillevold
+2  A: 

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

RichardOD
Even if it's not, it should be!
Robert
I don't know if it was that exactly, but after reinstalling ie7 with the newest updates, it worked fine. Thanks.
Chris
That's good that it's fixed.
RichardOD