'my MVC5 project needs a way to persist data non-globally

Im working on a very large MVC5 web application, using Entity framework.

Fetching the same users and their data from DB everywhere you need to use it, is not sustainable. Therefore my first version was making a static class with all the important variables and methods that need to be accessible from all over the application. Needless to say, I found out why people say its a bad idea to use static classes in web. (although i dont understand WHY this behavior exists, and why its not client specific, that would seem like the obvious way that it should work, considering that nobody wants to share the data from one client to another, we have databases and services for that, stupidly designed.. but anyways).

So I changed the class to a regular class, and store it in TempData, and simply keep renewing it and passing it around to where i need it. But its not working properly and i often lose my instances. Despite calling .Keep() every single time i use it.

I need something better. And cookies, session and cache is not really a good idea, since its either hard to get rid of (cookies), difficult to control "when" it is reset or needs to actually drop its values (session and cache).

TempData is to unreliable.

Anyways, what could I use that can solve my needs? Basically looking for some way to store a class with properties, variables and methods, that i can easily access in all c# / razor in the project, It should only live in the client's session, not be shared amongst other clients (like static classes apparently do), and since some of the data is sensitive info, should be relatively difficult to "steal".



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source