Carlos Miguel Colanta
Carlos Miguel Colanta

Reputation: 2823

Session disappears after restart while form authentication cookie stays

This is my configuration

Forms:

<authentication mode="Forms">
<forms loginUrl="~/wms/login" timeout="100"/>
</authentication>

Session:

<sessionState mode="InProc" customProvider="DefaultSessionProvider" timeout="100">
  <providers>
    <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
  </providers>
</sessionState>

If the user will input the right credentials, they will get authenticated like this:

FormsAuthentication.SetAuthCookie(entry.username, false);

Nothing special, but my problem is that if i will restart from visual studio(stop then re-build): Most of the time, the user is still authenticated(which will be redirected to the next page) but my next page requires a session value and since the session is gone already, it will simply crash my application.

How do i fix this?

Upvotes: 0

Views: 874

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

How do i fix this?

By default Session State is stored in-memory (mode="InProc") and it will not survive application restarts. You might need to use a distributed session state mode in order to persist those sessions in a state server or database. It's exactly for this purpose that in production you should absolutely never use in-memory session state.

Checkout the different state modes: https://msdn.microsoft.com/en-us/library/ms178586.aspx

Here's what you need know when you switch to a distributed session state mode: all objects that you store in the session along with their entire object graph needs to be decorated with the [Serializable] attribute. They will be serialized using the BinaryFormatter class in order to be transported over the network to the state server you have chosen.

Upvotes: 2

JOSEFtw
JOSEFtw

Reputation: 10071

The session gets cleared from the server everytime you stop and rebuild. Everytime you stop/start the application it will "recycle". Im guessing you are running on IIS Express, in that case, everytime you stop the application, the process is destroyed, hence, your session is gone.

You can maybe find some useful information here: asp.net - maintain session while debugging and rebuilding solution?

Upvotes: 1

Related Questions