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

推荐订阅源

博客园 - 【当耐特】
Help Net Security
Help Net Security
P
Proofpoint News Feed
J
Java Code Geeks
爱范儿
爱范儿
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
Google DeepMind News
Google DeepMind News
H
Help Net Security
G
Google Developers Blog
Jina AI
Jina AI
Vercel News
Vercel News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
Lohrmann on Cybersecurity
S
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
Security Archives - TechRepublic
Security Archives - TechRepublic
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic
GbyAI
GbyAI
B
Blog
O
OpenAI News
博客园_首页
Cisco Talos Blog
Cisco Talos Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
M
MIT News - Artificial intelligence
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
Webroot Blog
Webroot Blog
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
C
Cisco Blogs
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
AI
AI
W
WeLiveSecurity
aimingoo的专栏
aimingoo的专栏
The Register - Security
The Register - Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale

博客园 - Fernando

div 图片垂直居中问题 连续字符自动换行的解决方案 border: none 0 如何使用fiddler在firefox下通过2层代理调试网页 CSS hacks *:first-child+html CSS Hack 【代码美化】CSS代码格式化和JS代码格式化工具 Fiddler的几个使用技巧 如何直接调试线上页面的JavaScript和CSS 敏捷软件开发模型--SCRUM 第一章:逻辑与证明 第九城市 Cognizant 高知特 上海瑞润电子科技有限公司 上海梵科信息科技有限公司 班库(上海)商务咨询有限公司 上海蓝矩信息科技有限公司 C# 中的委托和事件详解 无法打开 open a new function library
关于Session_End()运行机制的一些细节!
Fernando · 2008-08-01 · via 博客园 - Fernando

1.首先是如何激发Session_End()方法
因为这个方法只支持InProc(进程内的)类型的Session,所以我们将Web.config配置如下:

<sessionState timeout="1" mode="InProc">
</sessionState>

注:timeout的基本单位是:分

2.为什么执行了Session.Abandon(),但是却仍然可以从Session中取到值?
   为什么在Session_End()中,无法获得HttpContext.Current对象?
按示例说明:
aspx部分

    <form id="form1" runat="server">
        
<div>
            
<asp:Literal ID="lblMsg" runat="server"></asp:Literal>
            
<br />
            
<asp:Button ID="btnTest" runat="server" Text="注销Session" OnClick="btnTest_Click" />            
            
<asp:Button ID="Button1" runat="server" Text="回发" />
        
</div>
    
</form>

aspx.cs部分

    protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!Page.IsPostBack)
        
{
            
this.Session["state"= 1;
        }

        
this.lblMsg.Text = Convert.ToInt32(Session["state"]).ToString();
    }


    
protected void btnTest_Click(object sender, EventArgs e)
    
{
        
this.Session.Abandon();
        
// 重新设置lblMsg.Text
        this.lblMsg.Text = Convert.ToInt32(Session["state"]).ToString();
    }

Global.ascx部分

    void Session_End(object sender, EventArgs e) 
    
{
        
try
        
{
            HttpContext.Current.Response.Write(
"调用Session_End()方法");
        }

        
catch
        
{
            
throw new Exception("捕获异常");
        }

    }

运行测试:
1)运行程序发现,点击了“注销Session”按钮后,即使重新设置lblMsg.Text,输出的值也仍然为1,并没有像我们预期认为的那样,应该是Session被注销,然后通过Convert.ToInt32对NULL值的转换后,输出0。
2)现在给ASPX页添加一个新的BUTTON控件,重新运行程序,依次点击“注销Session” - “回发”,发现在第2次点击后,结果输出0。通过调试也发现在调用Session.Abandon()后,的确进到了Session_End()方法。
注:通过调试发现,第一步和第二步均可以激发Session_End()方法

调试分析:
1)前置条件:点击“注销Session”按钮
给btnTest_Click()内的代码设置断点,通过调试可以发现,其中执行的顺序并
不是:从Session.Abandon() - 到Session_End() - 执行完之后再返回到btnTest_Click()中继续执行其他事件
而是:将btnTest_Click()中所有事件执行过后 - 再转去执行Session_End()
2)前置条件:无
在Session_End()中设置断点,然后正常启动页面,因为我将sessionState的timeout设置为1分钟,所以干脆什么都不做,等1分钟过去时,发现程序自动进到Session_End(),执行到这里思路应该就比较清楚了,正如cnblogs很多贴子中所说的“

Session_End()是一个在服务器内部激发的事件处理函数,它是基于一个服务器内部的计时器的”,因为在激发该事件时服务器上并没有相关的HttpRequest对象,所以也不存在HttpContext这个概念。至于为什么客户端需要通过再一次Request回发请求,才会得到0,根据上面的第一步来看也就很明白了。

总结:
1)

要激发Session_End(),必须正确配置Web.config,如<sessionState timeout="1" mode="InProc" />
2)Session_End()中是无法获得HttpContext对象的。
3)执行Session.Abandon()后,客户端必须至少有一次请求,才能正确反应Session的状态。