I am coding an MVC 5 internet application and when a user logs in, I wish to store the log in details so that I can retrieve these details in any class.
I have done some research, and I can either cache the data, or store this data in a session. Because the data is only set when the user logs in, and is only relevant to the logged in user, I believe that I should use the following code:
Session[key] = data;
What are the disadvantages or using the above code? Is there a timeout for this session data, or does it persist until the user closes the browser and/or logs out?
The other option is to use the following code:
System.Web.HttpRuntime.Cache[key] = data;
Are there any advantages that I should consider when using the above code? I am currently leaning towards using the Session[key] = data, as the data is only relevant to the logged in user, and is not application wide.
Thanks in advance.
Session is per user/session while cache is per application. So if I understand well, cache couln't be used for you, isn't it?
Session expires when user close the session, or you can create a session without the login or the user. You set the maximum time for the session on the web.config. Session close when user close the browser.
If you want to make the session available too after browser closing, you have to mix cookies and session. Cookies are stored into the client's browser and can host the Id of the sesssion but I wouldn't recommand it.