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:
- Copy the code below into an asp.net page.
- Use IE (I used IE7, firefox and chrome don't appear to have this issue)
- Notice that the session is new.
- Refresh the page; notice that the session is not new.
- Download the file and save it.
- 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>