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

ASP.NET
二级域名Cookie问题的解决方法
如何为asp.net网站项目添加子项目
asp.net用url重写URLReWriter实现任意二级域名
asp.net 序列化and反序列化演示
asp.net Timer的使用方法
AjaxControlToolKit DropDownExtender(下拉扩展控件)使用方法
AjaxControlToolKit CalendarExtender(日历扩展控件)的使用方法
让GridView只显示特定用户的数据的方法
让GridView只更新某些特定的数据的方法
ajaxControlToolkit中CascadingDropDown的用法说明
axp.net ScriptManager的简单用法
把程序集安装到全局程序集缓存中的步骤
引用全局程序集缓存内的程序集的方法
asp.net COOKIES需要注意的一点
asp.net UrlReWriter使用经验小结
页面导出为Excel的时间格式的问题
asp.net cookie清除的代码
Asp.net XMLHTTP封装类(GET,Post发送和接收数据)
ASP.NET XmlHttp跨域访问实现代码
Asp.NET 随机码生成基类(随机字母,随机数字,随机字母+数字)

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


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