浏览器窗口关闭时,Asp.Net 会话变量不会被放弃?
这是一个奇怪的问题,不确定发生了什么。我正在使用 IIS7 和 .net 4.0。
它似乎可以防止浏览器关闭后会话 cookie 保留在客户端上。另一个奇怪的是,我可以关闭 IE 并打开 FireFox,会话也在那里。怎么样?我想这就是我的环境的设置方式。我为 InProc 设置了会话并在 IIS 中使用 cookie。 IIS7 有什么不同?
更新:我正在为我的应用程序池使用集成模式。我查看了一个使用 .net 3.5 和 iis6 创建的旧站点,该站点在 Sever2003 上运行。我可以登录该网站,它会为我创建会话变量。然后我转到 FireFox 并打开同一站点。它要求我登录(如果会话存在,我的应用程序会将您带到您的 prfile)。如果我关闭 IE 并重新打开 IE,然后返回我的网站,则需要我再次登录。 iis7 和我当前的应用程序发生的情况非常奇怪。我的会话提取方式的唯一区别是我在将当前处理程序转换为 Page 对象时获取变量: (Page)HttpContext.Current.Handler
更新:好吧,我想我找到了问题所在,并且必须将 HttpContext.Current.Handler 强制转换为当前页面对象。我有一个配置文件,我想在其中放置一个属性,以便所有其他类可以引用一个中心点来获取我创建的用户会话对象。 HttpContext.Current.session 始终为 null,有人建议强制转换 HttpContext.Current.Handler。我创建了一个简单的页面,用于检查会话变量是否已创建,如果没有,则创建它。然后我打印出该值。当我关闭浏览器时,会话就消失了。所以,这是有效的。我最初在这条消息中使用的代码实际上是用于后退按钮的,但我不需要感谢安德鲁指出这一点。所以我想不清楚为什么从 Hnadler 中提取的会话始终可用,直到我专门清除它。
This is a strange issue and not sure what is going on. I am using IIS7 and .net 4.0 to being.
It seemed to keep the session cookies from staying on the client after the browser was closed. Another strange this is that I can close the IE and open FireFox and the session is in there too. HOW IS THAT? I am thinking it is how my environment is setup. I have the sessions set for InProc and using cookies in IIS. What is different in IIS7?
Update: I am using integrated mode for my app pool. I looked at an older site I created using .net 3.5 and iis6 running on Sever2003. I can log into the site and it creates the session variable for me. I then go to FireFox and open the same site. It requires me to log in (my application will take you to your prfile if a session exists). If I then close IE and reopen IE, then go back to my site, it requires me to log in again. What is happening with iis7 and my current application, is quite odd. The only difference in how my session is pulled is that I am getting the variable while casting the current handler to the Page object: (Page)HttpContext.Current.Handler
Update: well, I think i found where the issues is and it has to do with casting the HttpContext.Current.Handler to the current page object. I have a configuration file where I wanted to put a property so all other classes could reference a central point to grab the User session object I created. The HttpContext.Current.session was always null and someone had suggested casting the HttpContext.Current.Handler. I created a simple page that checks to see if a session varaible has been created and if not it creates it. Then I print out the value. When I close the browser, the session is GONE. So, that is working. The code I had origianlly in this message was really for the back button, but I am not in need of that thank for Andrew pointing that out. So I guess it is not clear why that session pulled from the Hnadler is always available until I speicifcally clear it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
会话在指定的不活动时间(默认 20 分钟)后结束。 Http 是无状态的,服务器与保持浏览器打开但不会向关闭浏览器窗口的人发出任何请求没有什么不同。
Session_OnEnd 仅在 InProc 会话状态下触发。 IE 和 Firefox 共享会话的唯一方法是无 cookie 会话,其中会话 ID 嵌入在 url 中,并且您可以在浏览器之间复制 url。
A session is ended after a specified amount of inactivity (default 20 minutes). Http is stateless, the server can not differ from someone who leaves the browser open but doesn't issue any requests from someone who closes their browser window.
Session_OnEnd is only triggered for InProc session state. Only way for IE and Firefox to share session would be cookieless sessions, where the session id is embedded in the url, and you copy the url between browsers.
尝试以下方法来清除您的会话:
Try this to clear your Session:
好吧,我从来没有得到这个问题的答案,并且仍然不确定为什么 HttpContext.Current.Handler 总是保留我的会话。因此,我最终改变了设置和获取会话变量的方式。感谢大家看到这里。
Well, I never got an answer to this and still not sure why the HttpContext.Current.Handler was always holding onto my session. So, I ended up changing how I was setting and getting my Session variable. Thanks to everyone for looking at this.