Asp.net Sessions的详细和具体使用?

发布于 2025-01-06 21:39:19 字数 228 浏览 1 评论 0原文

任何人都可以帮我解释 ASP.NET 会话的详细和正确使用吗?

我阅读了许多门户网站和博客,但我不知道如何以及在哪里使用这些会话。

我们在页面上创建许多会话,用于登录,将一些值从一个页面传输到另一个页面。但它对多个用户(例如超过10000个用户访问网站)、服务器传输速率有什么影响。内存存储等。

这可能会帮助许多初学者和有经验的人在他们的项目中正确使用会话。

任何帮助表示赞赏。

Can any one help me in explaining the detailed and proper use of ASP.NET Sessions.

i read many web portals and blogs but i do not understand how to and where to use the sessions.

we create many sessions on page, for login, transfering some values from one page to another. but what is its impact on multiple users like more than 10000 users accessing the website, server transfer rate. memory storage, etc.

This may help many beginners, and also experienced person to properly use sessions in their project.

Any help is appreciated.

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

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

发布评论

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

评论(1

爱格式化 2025-01-13 21:39:19

其工作原理大致如下:

当用户访问您的网页时,会话 ID 会设置在用户浏览器的 cookie 中。每次浏览器向服务器发送请求时,浏览器都会将包含会话ID的cookie传递给服务器。这允许服务器识别用户并将数据与多个页面请求中的用户关联起来 (如果您愿意,您可以使用没有 cookie 的会话)。

服务器默认将这些数据存储在内存中。但是,如果多个网络服务器正在运行应用程序并为同一用户提供服务,则它们都需要了解用户的会话数据。因此,您可以将应用程序配置为使用“ASP.NET State Server”Windows 服务存储会话数据,也可以将数据存储在 SQL 数据库中(或者您可以编写自己的会话状态提供程序并将数据存储在您喜欢的任何位置) )。此外,如果您担心计算机可能崩溃(这显然应该让您担心),那么将会话数据存储在内存中显然是一个糟糕的选择。

至于 ASP.NET 会话的“正确且详细”的使用,很难说 - 这取决于您想要实现的目标。
如果可以的话,您应该只在会话中存储少量数据,因为访问您网站的所有用户的合并会话可能会占用相当多的空间。此外,如果您使用 ASP.NET 状态服务器或 SQL Server 会话状态存储,那么您存储的数据需要序列化和反序列化,对于大小不小的数据来说,这将花费不小的时间。

如果您计划存储的内容不是保密的,另一种方法可能是将数据存储在 cookie 中。这样您的服务器就不必担心存储数据。这样,您就可以用内存(或磁盘空间或您选择的任何存储机制)来换取带宽,因为 cookie 现在将成为每个请求的有效负载的一部分。

This is roughly how it works:

When the user visits your webpage, a session ID is set in a cookie in the user's browser. Each time the browser sends a request to the server, the browser will pass the cookie containing the session ID to the server. This allows the server to recognize the user and associate data with the user across multiple page requests (you can use sessions without cookies if you want to).

The server will by default store this data in memory. However, if multiple webservers are running the application and serving the same user, they will all need to know about the user's session data. Thus, you can configure your application to store session data using the "ASP.NET State Server" Windows service, or you can store the data in a SQL database (or you can write your own Session State Provider and store the data wherever you like). Moreover, storing the session data in memory is obviously a bad choice if you are worried your machine might crash (that obviously should worry you).

As for the "proper and detailed" use of ASP.NET sessions it is hard to say - it depends on what you are trying to achieve.
If you can help it, you should store only small amounts of data in sessions, as the combined sessions of all users visiting your website may take up quite a lot of space. Moreover, if you are using the ASP.NET State Server or the SQL Server session state stores the data you store needs to be serialized and deserialized, which will take a non-trivial amount of time for data of non-trivial size.

If what you are planning to store isn't confidential, an alternative approach might be to store the data in a cookie. That way your server will not have to worry about storing the data at all. This way you are trading memory (or disk space or whatever storage mechanism you choose) for bandwidth, as the cookie will now be part of the payload for every request.

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