简体   繁体   English

asp.net mvc Azure云服务,我应该使用TempData还是Sessions来保存一些用户信息或其他内容

[英]asp.net mvc Azure cloud services, should I use TempData or Sessions to hold some user info or something else

I am developping a website that will be hosted on azure cloud services. 我正在开发一个将在Azure云服务上托管的网站。 I am wondering about the best way to save some information of the logged user (basically some Ids, and a list/dictionary). 我想知道保存已登录用户的一些信息的最佳方法(基本上是一些ID,以及一个列表/词典)。

The app could be deployed on multiple instances. 该应用程序可以部署在多个实例上。 I know for sure that static variables are not a solution. 我肯定知道静态变量不是解决方案。

So is it safe to use Sessions, should I go with TempData (using the peek method or just making a wrapper class that will use the peek method) or something else? 因此使用Session是安全的,我应该使用TempData(使用peek方法还是仅创建将使用peek方法的包装器类)或其他方法?

Basically I would like to avoid calls to the database to retrieve same data because itself is reached through a webservice. 基本上,我想避免调用数据库来检索相同的数据,因为它本身是通过Web服务到达的。

data stored in TempData will be cleared in the next request, it's just a temporary store (eg when you want to maintain data when doing redirect), so it's not a good place to put data that is valid for the whole session TempData中存储的数据将在下一个请求中清除,它只是一个临时存储(例如,当您在重定向时想要维护数据时),因此不是放置对整个会话有效的数据的好地方

Normally user information is stored in 2 ways after successfully log in: 成功登录后,通常以两种方式存储用户信息:

  • Using session: it's safe because the data is stored on server (if using in-proc session) or on another sever (if using distributed session). 使用会话:这是安全的,因为数据存储在服务器上(如果使用进程内会话)或另一个服务器(如果使用分布式会话)。 For cloud, you should use Azure cache service or Azure Redis as session provider 对于云,您应该使用Azure缓存服务或Azure Redis作为会话提供程序
  • If amount of user information is small, you can serialize it and put in authentication cookie. 如果用户信息量很少,则可以对其进行序列化并放入身份验证cookie。 At each request, client will send cookie to server, server will deserialize the data into custom principal and store as current user. 在每次请求时,客户端都会向服务器发送cookie,服务器会将数据反序列化为自定义主体并存储为当前用户。 The disadvantage is that size of cookie will be larger compared to first approach, so only use it when the amount of information is small. 缺点是cookie的大小将比第一种方法大,因此仅在信息量较小时才使用它。 And it's also safe because cookie will be encrypted 这也是安全的,因为cookie将被加密

TempData only stores data the time of your HTTP request. TempData仅在HTTP请求时存储数据。 Session is not very reliable, and is memory consuming, as long you don't have a shared Session provider, like a SQLSessionProvider. 只要您没有共享的Session提供程序(例如SQLSessionProvider),Session就不会非常可靠,并且会占用内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM