在 ASP.NET 中丢失会话数据 [英] Losing session data in ASP.NET

查看:45
本文介绍了在 ASP.NET 中丢失会话数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个在运行 .NET 1.1 的服务器上运行的 ASP.NET 站点移到了运行 .NET 2.0 的另一台服务器上.

I moved an ASP.NET site running on a server with .NET 1.1 to another server running with .NET 2.0.

在其中一个页面中,我有以下代码来检测过期的会话:

In one of the pages I have the following code to detect an expired session:

  protected void Page_Init(object sender, System.EventArgs e) {  

    if ( Session["XBCPEmail"] == null ) {
      Response.Redirect("signin.aspx?expired=yes");
      return;
    }
  }

(Session["XBCPEmail"] == null) 在一种意外情况下,在单击页面按钮之一后解析为 true(就像会话已过期).它只发生在一个按钮上.就像同一页面中的其他按钮一样,按钮事件处理程序以重定向到同一页面的代码结束:

(Session["XBCPEmail"] == null) is resolving as true (as if the session had expired) in one unexpected case, after clicking one of the buttons of the page. It happens with only one of the buttons. Just like other buttons in the same page, the button event handler ends with this code redirecting to the same page:

Response.Redirect("cpanel.aspx"); 

我检查过,在 Response.Redirect("cpanel.aspx"); 的时候 (string)Session["XBCPEmail"] 的值是一个有效的字符串,所以我不确定 Response.RedirectPage_Init 之间会发生什么,这可能导致 Session["XBCPEmail"] 变为空.

I checked and at the time of Response.Redirect("cpanel.aspx"); the value of (string)Session["XBCPEmail"] is a valid string, so I'm not sure what can happen between the Response.Redirect and the Page_Init that could be making the Session["XBCPEmail"] become null.

哪个可以使 .NET 2.0 中的 Session 变量变为空?这段代码在 1.1 中没有这个问题,即使在 2.0 中,它也只影响页面上的一个按钮.

Which could make a Session variable in .NET 2.0 become null? This code does not have that issue in 1.1 and, even in 2.0, it only affects one button on the page.

更新:仅当按钮事件处理程序使用以下代码调用外部 .exe 程序时才会出现此问题.如果此代码被注释掉,则 Session 变量不为空.创建外部进程来运行命令行程序对 Session 变量是否为 null 有何影响?

UPDATE: The issue only occurs if the button event handler calls an external .exe program, with the code below. If this code is commented out, the Session variable is not null. How can the creation of an external process to run a command line program have any impact on if a Session variable is null or not?

private string CallBridge3(string task, string arg1, string arg2, string arg3) {

    Process process = new Process();

    process.StartInfo.FileName = MapPath("bridgefcp.exe");
    process.StartInfo.Arguments = "-" + task + " \"" + arg1 + "\" \"" + arg2 + "\" \"" + arg3 + "\"";
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.UseShellExecute = false;

    process.Start();

    string output = process.StandardOutput.ReadToEnd();
    process.WaitForExit();
    return output;
  }  

更新 2:在装有 IIS 7.5 机器的 Windows 2008 R2 上安装 .NET 4.5 而不是使用默认的 .NET 2.0 后,问题就消失了.>

UPDATE 2: The problem has vanished after installing .NET 4.5 on the Windows 2008 R2 with IIS 7.5 machine, instead of using the one that came by default, which was .NET 2.0.

推荐答案

默认情况下 Response.Redirect 终止线程执行,并且在设置会话变量时可能存在竞争条件.它在文章 不要在设置 Session 变量后重定向(或正确操作),所以尝试使用另一个不那么暴力的版本:

By default Response.Redirect terminates thread execution and there might be a race conditions in setting session variables. It is described in article Don't redirect after setting a Session variable (or do it right), so try to use another, less violent version:

Response.Redirect("cpanel.aspx", false); 

这篇关于在 ASP.NET 中丢失会话数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆