Saving user data in MVC 3

I was given the requirement to save user data after initial user authentication. We don’t want to get into the database to search for the user every time they switch to a new view, etc.

I have a User class that is [Serializable], so it can be saved in a session. I also use SQL server for session state. I thought about storing the object during the session, but I really don't like to do this.

How do developers handle this type of requirement these days?

+4
source share
2 answers

Three ways:

  • Encrypt data in cookies and send it to the client, decrypt it when you need it
  • Saving on the server side with an identifier (for example, UserId) in a cache, session, or any other storage (which is safer than a cookie).
  • Use a second level caching strategy if you used ORM
0
source

Assuming your custom object is not huge and doesn't change often, I think it can be stored in a session.
Since you already have a sql server session, you will make SP calls to pull / push data already and add a small object to it should have minimal problems with punching compared to other options, such as saving it to the client and sending it to each request. I also consider the server a safer place to store this information.
You want to minimize the number of times you write per session (lock request) when it is stored in sql, because it is implemented in a private class that exclusivity blocks the session. If any of your other queries in this session require write access to the SQL session, they will be blocked by the original query until it releases the session lock. (There are several new interceptors in .NET 4 that allow you to modify the SessionStateBehavior in the pipeline before access to the session)

You can consider a session state server (appfabric) if a problem in your SQL session store is a problem.

0
source

Source: https://habr.com/ru/post/1386763/


All Articles