Reputation: 55062
I have a base Controller which all the controllers extend, and at base controller I do some session handling. Such as storing user id etc.
Moreover, I have a User
class which has other classes such as :
class User
{
public int Id {set;get;}
public virtual Location Location {set;get;}
}
When i populate this from Database (i m using code first EF), obviously Location wont load, Should i store this in the session the way it s?
Main question is I am storing Id
in base controller and losing the session everytime i restart the application. why is that happening? i extended session timeout and didnt help.
What are the best practices for session handling? is there a wrapper i can use?
Upvotes: 0
Views: 2150
Reputation: 142
That what you need - add this code to web.config/system.web:
<machineKey validationKey="C5034160419189092507195D247C6FCD9F54D7A967372A23078E09F6440087328A874AD69955F441B526A265CC3A17CDEAAE8AB21A16868F549C3077C39C8E9F" decryptionKey="078FAD13FAC4E41EB0762F0B34E3F4990A144897C3387A70A746187F3AECD8DE" validation="SHA1" decryption="AES" />
<sessionState timeout="300" mode="InProc"></sessionState>
this machinKey was generated from http://aspnetresources.com/tools/machineKey
Upvotes: -1
Reputation: 15772
Re: storing User in session: You can, just make sure you don't ever access the 'Location' property without re-attaching it to a session. If you don't need the data, an option is to just copy the data you do need into a simpler object that doesn't have 'Location' and store that in your Session.
Re: losing the session context, that's because the default is to store session data in memory, which is lost whenever the application restarts. Take a look at using the StateServer mode for session if you want that state to persist across application restarts (though if you want to persist server restarts or work in a multi-server farm, you'll want to go the SQLServer route).
Upvotes: 3