当用户登录时获取数据的最佳方式是什么?

发布于 2024-09-23 23:55:06 字数 271 浏览 0 评论 0原文

在我的应用程序中,我需要在用户保持登录状态期间存储一些变量,这些变量用于提供有关用户如何查看其数据(预定义的过滤器、语言等)的定制体验。我需要的数据不超过1Kb。

我读过许多博客文章,明确鼓励不要将此数据存储在 Session 对象中。在许多这样的博客文章中,作者建议改用 TempData。

据我了解,TempData 对于短期临时数据来说是一个不错的选择,不适合在用户保持记录的整个期间缓存数据。

我有错吗?什么是适合我的场景的好的替代方案?

感谢您的帮助:)

In my application I have the requirement to store, for the period the user stay logged in, some variables that's used to provide a customized experienced on how the user views it's data (pre-defined filters, language, etc.). My needed data is not more than 1Kb.

I have read many blog posts that definitely encourage to not store this data in the Session object. In many of these blog posts the authors suggests to use TempData instead.

As I understand TempData is a good choice for short-lived temporary data and not suitable for caching data during all the period the user stay logged.

Does am I wrong? What is a good alternative suitable to my scenario?

thanks for helping :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪 2024-09-30 23:55:06

两个选项:

  1. Cookie
  2. 数据库

如果仅需要在用户登录时存储此信息,并且您不想在他回来时保留该信息,那么 Cookie 就可以正常工作。另一方面,如果您想保留用户的自定义设置,那么您需要将它们存储在数据库中或使用持久 cookie。

会话也是一个选项,但如果您的站点在网络场中运行,请小心 - 在这种情况下,您将需要进程外会话持久性。

据我了解,TempData 对于短期临时数据来说是一个不错的选择,不适合在用户保持登录的整个期间缓存数据。

你是绝对正确的。 TempData 应该仅在以下场景中使用:用户调用控制器操作,该控制器操作将某些内容存储到 TempData 中并立即重定向到另一个控制器操作(它从不渲染视图),后者获取存储的内容数据并呈现视图(POST 场景后重定向)。

Two options:

  1. Cookies
  2. Database

If this information needs to be stored only for the time the user is logged in and you don't want to persist it when he comes back cookies would work just fine. If on the other hand you want to persist the user's customized settings then you need to store them in the database or use persistent cookies.

Session is also an option but be careful if your site runs in a web farm - in this case you will need an out-of-proc session persistence.

As I understand TempData is a good choice for short-lived temporary data and not suitable for caching data during all the period the user stay logged.

You are absolutely right. TempData should be used only in the following scenario: a user calls a controller action, this controller action stores something into the TempData and immediately redirects to another controller action (it never renders a view) which fetches the stored data and renders a view (Redirect After POST scenarios).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文