当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net(c#)有关 Session 操作的几个误区

ASP.NET
在图片上加入图片版权信息
Peer-to-Peer (P2P) communication across middleboxes(翻译4)
今天完成了.net compact framework 加 web service的演练.
Cordbg, Dumpbin, Ildasm, 的一些教程。
asp和asp.net的session共用
VB连接SQL数据库的模块
消除图片在ie中缓存而无法更新的问题
说说使用static和const关键字
怎样解决thephile中的数据库由于排序造成的问题:对 text 数据类型不支持代码页转...
.net分布式事务例子
Internet Explorer 编程简述(二)
使用SqlParameter参数返回值时遇到的问题
vb可不可以实现虚拟中断
C#下Socket对象的BeginReceive方法,执行后竟然不调用AsyncCallback里的回调函数
坚持学asp.net:(十一)
[C#][正则表达式]寻找匹配的Groups的几种方法
面向服务的体系结构概述
Windows Form 和 UserControl
VB中類模塊實現與C++中類實現的比較(1)
下载Oracle数据库中的Blob二进制文件,实例!

ASP.NET 中的 asp.net(c#)有关 Session 操作的几个误区


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 208 ::
收藏到网摘: n/a

1. this.Session["username"] = null
HttpSessionState 内部使用 NameObjectCollection 类型的集合对象来存储用户数据。因此使用 this.Session["username"] = null 仅仅是将该元素的值设为 null 而已,并没有真的将其从 Session 中移除。(为什么?晕~~~ 建议看看 C# 基础方面的书。)
正确的方法是:this.Session.Remove("username");
删除全部数据:this.Session.RemoveAll(); 或 this.Session.Clear();
2. this.Session.Abandon()
该方法会导致当前 Session 被取消,系统会触发 Global.asax 中的 Session_End 事件(仅限于 Mode = InProc 时)。
尽管再次发出请求时 SessionID (可能)没有发生变化,但是你会发现 Global.asax Session_Start 事件被触发。你还可以使用 this.Session.IsNewSession 属性来判断当前 Session 是否重新创建的。
由于某些组件和控件可能要使用 Session 信息(如使用 this.Session.SyncRoot 进行同步),因此不要轻易使用该方法清理 Session。
3. 用户身份验证
不要使用 this.Session["username"] = "ZhangSan" 、if (this.Session["username"] != null) 这样的方式进行用户身份验证,这种方式既不安全也不合理。有关身份验证请参考 MSDN 文档或雨痕写的相关文章。