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

ASP.NET
细细品味ASP.NET(一)
细细品味ASP.NET(二)
细细品味ASP.NET(三)
细细品味ASP.NET(四)
细细品味ASP.NET(五)
用asp.net和xml做的新闻更新系统(1)
用asp.net和xml做的新闻更新系统(2)
用asp.net和xml做的新闻更新系统(3)
十天学会ASP.net(1)
十天学会ASP.net(2)
十天学会ASP.net(3)
十天学会ASP.net(4)
十天学会ASP.net(5)
十天学会ASP.net(6)
十天学会ASP.net(7)
十天学会ASP.net(8)
十天学会ASP.net(9)
十天学会ASP.net(10)
ASP.NET创建XML Web服务全接触(1)
ASP.NET创建XML Web服务全接触(2)

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 153 ::
收藏到网摘: 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 文档或雨痕写的相关文章。