How to clear System.Windows.Forms.WebBrowser session data?

user54045 picture user54045 · Jan 12, 2009 · Viewed 51k times · Source

How can I clear current session data (cookies, cached data, auth sessions, etc) without restarting the application?

Update: I'm talking about WebBrowser control in Windows.Forms, not the ASP.Net session.

Answer

user227719 picture user227719 · Dec 9, 2009

To clear session (such as HttpOnly cookies), you can use InternetSetOption() from wininet.dll.

private const int INTERNET_OPTION_END_BROWSER_SESSION = 42;

[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);

and use this method whenever need to clear session.

InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0);
webBrowser1.Document.Window.Navigate(url);