A sessão expira após o download do arquivo
-
21-09-2019 - |
Pergunta
Quando um usuário baixa um arquivo no meu aplicativo ASP.NET, a sessão expira alguns segundos após o download do arquivo. Antes que a sessão expire que possa executar qualquer tarefa, mas após cerca de 5 a 10 segundos, a sessão é reiniciada e eles são registrados.
Eu criei uma página simples para demonstrar isso. Para executar esta página simples, crie um novo projeto ASP.NET C# e insira o código em uma nova página.
EDITAR: Isso parece ser um problema do IE7. Firefox e Chrome não são afetados.
Eu acredito que o código responsável pela reinicialização da sessão é:
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();
Para recriar este problema:
- Copie o código abaixo em uma página do ASP.NET.
- Use o IE (usei o IE7, o Firefox e o Chrome não parecem ter esse problema)
- Observe que a sessão é nova.
- Recarregue a página; Observe que a sessão não é nova.
- Faça o download do arquivo e salve -o.
- Aperte o botão "Atualizar a página" algumas vezes até que a sessão "IS New" seja redisplay (cerca de 10 segundos).
Abaixo está o código para a recreação simples:
<%@ 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>
Solução
Algo a ver com isso (poderia ser um problema semelhante)? Talvez use Fiddler para ver o que está acontecendo com mais detalhes para o cookie.
Outras dicas
Certifique -se de começar com um navegador fresco depois de se livrar da resposta.Clear (). Eu tive o mesmo problema exato, leia este post e me livrei da resposta.Clear () funcionou, mas somente depois de despejar o navegador que estava aberto na minha área de trabalho e abri uma nova instância. boa sorte!
HM. Poderia ser isso Response.Clear()
Come o cookie do estado da sessão?