Microsoft KB Archive/311072

= BUG: Session ID Is Lost When You Close a Browser Window =

Article ID: 311072

Article Last Modified on 8/7/2003

-

APPLIES TO


 * Microsoft Internet Explorer 5.5

-



This article was previously published under Q311072



SYMPTOMS
If you close a Web browser window that is hosted in Microsoft Visual Basic, Microsoft Foundation Classes (MFC), or other types of applications while you keep the process alive, you may lose cookies that are issued without an expiration date. Cookies that are issued without an expiration date are known as session cookies. If you lose sessions cookies, you lose your Microsoft Active Server Pages (ASP) and Microsoft ASP.NET session state.



CAUSE
Internet Explorer ends the Internet session when you close the last browser window in the logon session.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



RESOLUTION
Make sure that the Internet session is not ended when you close the last window. Set the DISPID_AMBIENT_OFFLINEIFNOTCONNECTED ambient property to VARIANT_TRUE so that Internet Explorer does not check an internal session count variable. For example, use the following sample code in an MFC application: BOOL CGenhtmlviewView::OnAmbientProperty(COleControlSite* pSite, _    DISPID dispid, VARIANT* pvar) {   USES_CONVERSION; if (dispid == DISPID_AMBIENT_OFFLINEIFNOTCONNECTED) {       pvar->vt = VT_BOOL; pvar->boolVal = VARIANT_TRUE;
 * 1) include 

return TRUE; }   return CHtmlView::OnAmbientProperty(pSite, dispid, pvar); }



MORE INFORMATION
Internet Explorer maintains a session count that tracks the number of instances of the browser window in the logon session. Whenever the logon session count becomes 0, the process that closes the last window ends the Internet session by calling the InternetSetOption function (INTERNET_OPTION_END_BROWSER_SESSION). This technique causes the symptoms that can appear in two common situations:
 * When you host the WebBrowser control

Run an application, and then open a window that hosts the WebBrowser control. When you locate a Web site that maintains a session and then close the window of that session, if that window was the last window that displayed the WebBrowser control in all of the logon session, your memory cookies disappear, including your session ID. In the same running process, when you reopen that window and return to the site, you are in a different session.
 * When you start Internet Explorer from a file-based URL instead of an HTTP-based URL

You may lose your session state if you follow these steps:
 * Set your home page to C:\MyDoc\MyHome.htm.
 * Make sure that you have no active instances of either Internet Explorer or the WebBrowser control.
 * Click a URL that maintains a session ID to start Internet Explorer, and then locate a Web site.
 * Open a new window, which becomes the second window in the logon session, and then close the window.

NOTE: You may not lose your session state if your computer is on for several hours. Various conditions can cause Internet Explorer to lose track of its session count over the time of the logon session.

The problem does not occur if another instance of Internet Explorer is running in another process. Internet Explorer maintains its session count for all of the computer. Sometimes, you may not experience the problems that are described in this article. This is because some situations cause Internet Explorer not to decrement the session count. The longer your logon session, the more likely you are not to experience these problems.

