在asp.net中从会话状态保存和检索值 [英] saving and retrieving values from session state in asp.net

查看:47
本文介绍了在asp.net中从会话状态保存和检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看一些Web代码,但我不确定ASP.net会话状态如何工作.任何帮助将不胜感激.

I am reviewing some web code and I am not exactly sure how ASP.net session state works. Any helps would be gratefully appreciated.

如果在登录期间将User对象保存到会话状态,并且设置了User.FirstName和User.LastName.如果其他网页从会话中检索用户对象并将FirstName设置为其他名称,该名称是否会持久存在于其他网页上?或者,一旦修改了用户对象,是否需要将其重新添加到会话中?谢谢

If a User object is saved to the session state during login, and User.FirstName and User.LastName is set. If other web pages retrieve the user object from the session and set the FirstName to something else is that persisted on other web pages? Or, do you need to re-add the user object back to the session once it has been modified? Thanks

推荐答案

会话在服务器上保留,但通过客户端跟踪.我重复一遍-通过客户端.

Session is persisted on the server, but tracked via the client. I repeat - via the client.

在大多数情况下,使用Cookie跟踪会话.因此,以您的示例为例,当User对象保存到Session时:

In most cases, sessions are tracked with cookies. So using your example, when User object is saved to Session:

Session["UserInfo"] = new User { FirstName = "Joe", LastName = "Bloggs" };

Cookie将使用唯一的标识符发送给客户端.除非该cookie过期,否则它将一直传递到该客户端/浏览器发出的所有其他HTTP请求.

A cookie will be sent to the client with a unique identifier. This cookie is passed along to all further HTTP requests from this client/browser unless it expires.

如果另一个用户(来自另一台计算机)是第一次出现,则 Session ["UserInfo"] 将为空.

If another user comes along (from a different machine) for the first time, Session["UserInfo"] will be null.

Cookie的替代方法是"cookieless-session",即在其中不使用cookie来存储会话标识符,而是将标识符附加到URL上.

An alternative to cookies is "cookieless-session" - where instead of using a cookie to store the session identifer - the identifier is tacked onto the URL.

因此答案是否定的-其他网页(例如其他客户端/计算机/浏览器)将无法访问此信息.

So the answer is no - other web pages (e.g other clients/machines/browsers) will not have access to this information.

如果要从Web服务器在不同客户端之间共享信息,请使用缓存.

If you want information shared between different clients from the web server, use Cache.

但是,考虑到问题的上下文(用户信息),将其存储在会话中是有效的,因为它仅与特定用户相关(不应共享).

However given the context of the question (User information), it is valid to store this information in the Session, as it is only relevant to a particular user (shouldn't be shared).

很多人使用的替代方法是将其放在通用的原则中,而不是将用户信息粘贴在会话中,而该原则已附加到表单身份验证票证上.

An alternative a lot of people use instead of sticking the User info in the session, is to put it in a generic principle which get's attached to the Forms Authentication ticket.

由您决定选择哪一条路.

Up to you which road you choose.

这篇关于在asp.net中从会话状态保存和检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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