Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Losing session data in ASP.NET

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

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.

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.

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;
  }  

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.

like image 508
Pep Avatar asked Nov 09 '13 00:11

Pep


6 Answers

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); 
like image 148
Konrad Kokosa Avatar answered Oct 21 '22 22:10

Konrad Kokosa


Check your web.config, maybe you have this tag

<httpCookies requireSSL="true" />

If it so, remove it.

like image 42
Davor Avatar answered Oct 22 '22 00:10

Davor


You need to update web.config as mention below :

<httpCookies requireSSL="false" />
like image 23
Amol Ahirrao Avatar answered Oct 22 '22 00:10

Amol Ahirrao


I was facing the same issue and tried every option mentioned in the above answers. Finally found that the issue was that we had marked session cookie as secure in our project but were running it with http If the server has not been setup for SSL and you try to mark the cookie secure, a new session will be generated for each request. So finally enabling back https fixed it for me.

like image 2
Shikhar Arora Avatar answered Oct 21 '22 23:10

Shikhar Arora


I believe your session in the web.config is being reset (getting a new SessionID for each postback)

You could try to debug this by putting the SessionID somewhere on the page (for testing) with

HttpContext.Current.Session.SessionID

This did happen on one of my websites and all i had to do was go into IIS and resave the SessionState Panel

like image 1
Qpirate Avatar answered Oct 21 '22 23:10

Qpirate


Just go to your web.config file and edit your sessionstate tag. Set requiressl to false from true.

like image 1
Waleed Sheerani Avatar answered Oct 21 '22 23:10

Waleed Sheerani