ASP.Net Store User Data in Auth Cookie

后端 未结 6 582
情歌与酒
情歌与酒 2020-12-13 19:31

I want to store some data like the user nickname and user ID (table primary key) in the user data section of the auth cookie. The reason I\'m doing this is to retain this da

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 19:49

    Storing extra user data in the cookie means a larger cookie that is sent back and forth from the client with every request.

    A better approach to avoid the extra database hits you are worried about is to cache that data in memory after the user logs in.

    ASP.NET has a per request cache HttpContext.Current.Items and a app domain cache HttpContext.Current.Cache, I think you are looking for HttpContext.Current.Cache in this instance.

    Alternatively if you need caching across web servers (load balanced web servers) you can look into 3rd party key value stores like like memcached, redis, velocity, ncache etc.

提交回复
热议问题