user516883
user516883

Reputation: 9378

Session variable loses value in asp.net application

In my asp.net application I have a few session variables. Seems like only a few minutes on the localhost and also web host, they lose value. This happens periodically not all of the time. I do have cheap go daddy, web hosting, and that could be the problem on the web server. Thank you for any help, this is a big problem.

   <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="3600" defaultUrl="~/" name="" />
    </authentication>
    <sessionState mode="InProc" cookieless="false" timeout="80" />

 public static User User
    {
        get
        {
            User userLogin = null;
            object sessionVar = Session[USERLOGIN];
            if (sessionVar != null)
            {
                userLogin = (User) sessionVar;
            }
            return userLogin;
        }
        set { Session[USERLOGIN] = value; }
    }

Upvotes: 1

Views: 4449

Answers (2)

Riana M.
Riana M.

Reputation: 422

If I could comment I would, apparently I am not special enough ;P lol. I have to agree with KodeKreachor and Aristos on both points. I am not sure why there is a business/reasonable need to have the session state kept in memory.

To add to the points, mostly because I hate cookies, you could roll a solution to keep session states logged in a light weight database. I mean it really is all a cookie is for, flat file database record of information. SQLite might be a good solution, though unless I can find otherwise, you are rolling your own solution on that. Yes, as a loving Godaddy supporter, their cheaper services have a LOT to be desired. There is little to no control over what the system is doing with their shared hosting. My experience of hours talking to their tech department about what is and is not allowed can tell you that.

I would also mention that you might want to add some very verbose logging into the system using a solution such as Log4Net and then parse the logs for information on what is actually happening when the session state is being wiped. Then armed with that information move forward to find a much more clear solution.

Or just go easy on yourself and use cookies ;)

Just modify this for SQLite =D
HOW TO: Configure SQL Server to Store ASP.NET Session State

Of course here is a SQLite Session State Provider
SQLiteSessionStateStore

Apache Log4Net

Great Tutorial Series on using Log4Net by BeefyCode

Upvotes: 1

KodeKreachor
KodeKreachor

Reputation: 8882

There's a strong chance that your session is being wiped out on the server from the application pool being stopped and started, timeouts, or a host of other things. The main point is that session variables solely by themselves are unreliable persistent mechanisms.

If you need information to persist for a logged in user, try storing a cookie on the user's machine and if the session is null, repopulate it from identifying data in the cookie.

Upvotes: 4

Related Questions