今天发现在一个Asp.net站点中, 同一次登录, 不停刷新页面或者页面跳转, 此时后台Session的SessionID总是变化的.

创建一个页面,添加一个button,后台代码非常简单,如下

 
1
2
3
4
5
6
7
8
9
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(Session.SessionID);
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["userName"] = "bsbdfe123";
    }



前台就一个button按钮,当页面加载完成后,我尝试着做如下事情。

1、不停的刷新页面,sessionid一直会变。

2、不停的点击button,sessionid一直不变。

1.你开始不停的刷新的时候压根就没用去用session,所以服务器认为这个会话来源不同的用户。

2.当你使用了session的时候,服务器会向客户端发送一个包含sessionid的cookie,(button按钮点击事件里,使用了session.)会发现sessionID不变。

或者

 只要在Global.ascx中加入:

 

void Session_Start(object sender, EventArgs e) 
{
    // Code that runs when a new session is started

}

void Session_End(object sender, EventArgs e) 
{
    // Code that runs when a session ends. 
    // Note: The Session_End event is raised only when the sessionstate mode
    // is set to InProc in the Web.config file. If session mode is set to StateServer 
    // or SQLServer, the event is not raised.

}

就可保证同一次会话的SessionID不变.

posted on 2015-11-20 15:37  一切从新开始  阅读(4189)  评论(0编辑  收藏  举报