惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Fortinet All Blogs
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
小众软件
小众软件
有赞技术团队
有赞技术团队
雷峰网
雷峰网
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
AWS News Blog
AWS News Blog
C
Cisco Blogs
美团技术团队
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
人人都是产品经理
人人都是产品经理
宝玉的分享
宝玉的分享
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
W
WeLiveSecurity
D
DataBreaches.Net
博客园 - 司徒正美
Blog — PlanetScale
Blog — PlanetScale
IT之家
IT之家
云风的 BLOG
云风的 BLOG
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
Google DeepMind News
Google DeepMind News
T
The Blog of Author Tim Ferriss
Know Your Adversary
Know Your Adversary
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
Vercel News
Vercel News
月光博客
月光博客
T
Tailwind CSS Blog
H
Help Net Security
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
Microsoft Security Blog
Microsoft Security Blog
V
V2EX
WordPress大学
WordPress大学
Cyberwarzone
Cyberwarzone
Recent Announcements
Recent Announcements

博客园 - 马伟

通过xrdp实现远程桌面连接Windows Azure linux虚拟机 使用VNC远程连接Windows Azure Linux虚拟机 设置Windows Azure Linux虚拟机中的root账户 使用Windows Azure PowerShell远程管理Windows Azure虚拟机 将SQL Azure数据库备份到本地SQL Server 2012 asp.net MVC3 “System.Web.Mvc.ModelClientValidationRule”问题 在ASP.NET中以编程方式设置母版页 ASP.NET自定义输出缓存提供程序 ASP.NET缓存依赖--自定义缓存依赖 ASP.NET缓存依赖--SQL Server 2005与SQL Server 2008缓存依赖 QueryExtender控件之CustomExpression QueryExtender控件之OrderByExpression QueryExtender控件之PropertyExpression QueryExtender控件之RangeExpression QueryExtender控件之SearchExpession <<易学C#>>全书目录 用C#编程合并多个WORD文档 fckeditor编辑器上传文件出现invalid Request问题解决 荣获2009年“微软最有影响力开发者”
Session的生命周期
马伟 · 2012-04-11 · via 博客园 - 马伟

2012-04-11 21:52  马伟  阅读(5657)  评论()    收藏  举报

      我们已经知道,Session是在用户第一次访问网站的时候创建的,那么Session是什么时候销毁的呢?

      其实,Session使用一种平滑超时的技术来控制何时销毁Session。默认情况下,Session的超时时间(Timeout)是20分钟,即用户保持连续20分钟不访问网站,则Session被收回。如果在这20分钟内用户又访问了一次页面,那么20分钟就重新计时了。也就是说,这个超时是连续不访问的超时时间,而不是第一次访问后20分钟必过时。当然,你可以通过修改Web.config文件的配置项来调整这个超时时间,如下面的代码所示:

<sessionState timeout="30"></sessionState>

      你同样也可以在程序中进行设置,如下面的代码所示:

Session.Timeout = "30";

      一旦Session超时,Session中的数据将被回收,如果你再次使用Session,将给你分配一个新的SessionID。

      不过,你可别太相信Session的Timeout属性,如果你把它设置为24小时,则很难相信24小时之后用户的Session还在。Session是否存在,不仅仅依赖于Timeout属性,以下的情况都可能引起Session丢失:

      1)bin目录中的文件被改写。asp.net有一种机制,为了保证dll重新编译之后,系统正常运行,它会重新启动一次网站进程,这时就会导致Session丢失。

      2)SessionID丢失或者无效。如果你在URL中存储SessionID,但是使用了绝对地址重定向网站导致URL中的SessionID丢失,那么原来的Session将失效。如果你在Cookie中存储SessionID,那么客户端禁用Cookie或者Cookie达到了IE中Cookie数量的限制(每个域20个),那么Session将无效。

      3)如果使用InProc的Session,那么IIS重启将会丢失Session。同理,如果使用StateServer的Session,服务器重新启动Session也会丢失。

      如果你需要遍历当前的Session集合,你可以这样来处理,如下面的代码所示:

IEnumerator SessionEnum = Session.Keys.GetEnumerator();
while (SessionEnum.MoveNext())
{
    Response.Write(
Session[SessionEnum.Current.ToString()].ToString() 
+ "<br/>");
}

      有时候,我还需要立刻让Session失效。比如用户退出系统后,Session中保存的所有数据需要全部失效。处理方法如下面的代码所示:

Session.Abandon();