会话如何在asp.net 中工作? [英] How the session work in asp.net?

查看:31
本文介绍了会话如何在asp.net 中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人建议我在asp.net中会话是如何实际工作的?我在部分会话中感到困惑,想简要了解它,所以请指导我

Please any one suggest me how the session is actually work in asp.net? I am confuse in part of session and want to briefly knowledge of it so please guide me

推荐答案

ASP.NET 使用 cookie 来跟踪用户.当您第一次尝试向会话写入内容时,cookie 被发送到客户端,例如 ASP.NET_SessionId.此 cookie 由客户端在后续请求中发送.多亏了这个 cookie,服务器才能识别客户端并写入/读取相关的会话数据.重要的是要注意,这个 cookie 不是持久的(浏览器重启后将无法生存)并且与 HttpOnly 标志意味着客户端脚本无法访问它.

ASP.NET uses a cookie to track users. When you try to write something to the session for the first time a cookie is sent to the client, something like ASP.NET_SessionId. This cookie is sent by the client on subsequent requests. Thanks to this cookie the server is able to identify the client and write/read the associated session data. It is important to note that this cookie is not persistent (wouldn't survive browser restarts) and is emitted with the HttpOnly flag meaning that client scripts cannot access it.

除了 cookie 之外,您还可以将 ASP.NET 配置为使用隐藏字段或将会话 ID 附加到每个请求的查询字符串中.

In addition to cookies you could also configure ASP.NET to use hidden fields or append the session id to the query string on each request.

因此会话背后的基本思想是实际数据存储在服务器上的某个地方,客户端在每个请求上发送一些 ID,以便服务器知道在哪里可以找到它的数据.

So the base idea behind session is that the actual data is stored somewhere on the server and the client sends some ID on each request so that the server can know where to find its data.

默认情况下,有 3 个地方可以存储实际会话数据:

By default there are 3 places where the actual session data can be stored:

  • In-Proc:会话存储到应用程序的内存中(最快,但如果服务器场中有多个服务器,这将不起作用)
  • 过程外:数据存储在安装了状态服务的单独服务器中(数据存储在单独机器的内存中,这意味着多个 Web 服务器可以在一个 Web 场中工作)
  • SqlServer:数据存储在 SQL Server 中(它是最慢但最可靠的,因为会话数据存储在 SQL Server 数据库中,如果会话服务器崩溃可以提供服务,而 Out-Of-Proc 则不是这种情况)
  • 自定义实现:由于 ASP.NET 的可扩展性,您可以编写自己的会话提供程序并将数据存储在您喜欢的任何位置.

这是一篇关于 MSDN 的好文章,它探讨了 ASP.NET 会话状态.

Here's a good article on MSDN which explores the ASP.NET Session State.

这篇关于会话如何在asp.net 中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆