Mihai Popescu

Mihai Popescu

  • NA
  • 308
  • 152.5k

Session["UserId"] becomes null after random amount of time

Jan 21 2011 4:35 AM
Hello and thank you for showing your interest in helping me.

I have problems with my web session.

I on login i add the UserId like this:
Session.Add("UserID", UserInformation.UserId);

and on every operation i check if the user is logged in for the session

[WebMethod(EnableSession = true)]
public ResultInfo WebMethodName(Int32 isAdmin, Int32 userId, String SessionId)
{
        if (!IsLoggedIn(userId, SessionId))
        {
            Session.Abandon();

            // ...
        }
        // more code
}

private Boolean IsLoggedIn(Int32 userId, String sessionId)
    {
        if (!sessionId.Equals(Session.SessionID) || userId != getUserIdFromSession())
        {
            //must be false
            return false;
        }
        else
        {
            // some code
        }

        return true;
    }

    private Int32 getUserIdFromSession()
    {
        Int32 userId = 0;
        if (Session["UserID"] != null)
        {
            userId = Convert.ToInt32(Session["UserID"]);
        }

        return userId;
    }

It all worked fine on production server, beta server and local servers, but after an update on beta server after random periods of time Session["UserID"] becomes null. Sometimes right after login other times after a few operations, but usually if the  Session["UserID"] doesn't become null in the first minute then most likely it won't ever become until logged out. The weirdest thing is this only happens on the beta server and I can't reproduce it on local server. I tried to downgrade to a previous version but with no success. I must also mention that on the upgrade that might have produced this bug I also rewrote the config file from scratch but I checked with the data on the production server ant the config file looks fine. The web service is running on Windows Sever 2003.

If you have any idea on what might happen please let me know. If you need more info ask and I'll try to provide it.

Thanks in advance,
Mihai


Answers (3)