views:

396

answers:

2

I want to be able to preserve the state of the web browser control when an application is restarted. For example if I log into to a site with a two hour cookie expiration, I quit the app and restart it within the two hours, I would like to continue the same session. (the same way the session would be kept if I had the control open the whole time). Same thing with cookies that do not expire, which seem to be blown away.

Does anyone know of any solutions to this, or can point me into underlining activex code which I could use to save and restore the session? I'm pretty sure this is impossible through managed code.

+1  A: 

The cookies that do not expire are session cookies that are stored in memory, when the browser closes they are lost.

The session data is not stored in the cookie. It is stored on the server. The default session timeout is 20 mins.

But the cookie is the key to access the session.

So to get this to work, you need to:

  • Make sure that the cookie does not get lost, set an expiration
  • Make sure that the session information is not lost, set the default timeout of the session the same as the expiration of the cookie
Shiraz Bhaiji
That's good for the sever side, but my question is about the client side. When you quit an application with a webcontrol the cookies get blown away (session and regular). I want to be able to preserve this in between uses. Basically allowing a user to login once, quit the app, start the app again and still be logged in. Following all rules such as cookie expiration, etc.
Jakub
This is the correct answer. The information mentioned affects how the client side interacts with sessions that live on the server.
AaronS
+1  A: 
    [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool InternetGetCookie(
        string lpszUrlName,
        string lpszCookieName,
        StringBuilder lpszCookieData,
        [MarshalAs(UnmanagedType.U4)]
ref int lpdwSize
    );


    [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool InternetSetCookie(
        string lpszUrlName,
        string lpszCookieName,
        string lpszCookieData
    );

I'm currently working on testing this out, I'm not sure if it will work with the newer Vista/Win7 security mode. But should be a good starting point.

Jakub