Maintaining state in Asp.Net MVC website

前端 未结 2 1210
一个人的身影
一个人的身影 2021-01-22 12:51

I\'m currently designing a new website built on MVC and I wonder what is the right way to manage state.
The state should contain the userId and some structs of the user info

相关标签:
2条回答
  • 2021-01-22 12:58

    If you are using .NET 4.5 and dependent on the type and amount of information you are keeping on users you may want to look at using claims to store information about the user. In .NET 4.5 all Principals inherit from ClaimsPrincipal. ClaimsPrincipal already uses claims to store the user name, roles and other information. You can create your own service to transform claims, which will allow you to add additional information to the Principal user.

    0 讨论(0)
  • 2021-01-22 13:02

    (1) Use Sql Server to Store Session State

    (2) Use Memcached as a Session State Provider

    (3) Cook up your own solution using Caching on an external caching provider: look into using something like the ServiceStack Caching Framework. Using this, you can use Redis, Memcached, Azure or AWS to handle caching.

    Next, create a KeyFactory to handle generation of keys for specific items. The item keys would include the UserId (which you would always have from FormsAuthentication UserId (assuming that you are using FormsAuthentication). Then store any Session data for the user in the cache. Using this approach you are using Caching in place of Session, and the cache can be shared across multiple servers.

    Note: you can have different approaches regarding clearing out the user's data whenever they begin a new session. Potential approaches include:

    • Include the user's session start dateTime in the cacheKey, and auto-expire entries when they are no longer fresh
    • Clear out all potential entries for a user when they begin a new session
    0 讨论(0)
提交回复
热议问题