Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set and delete cookies from WebBrowser Control for arbitrary domains

How can I set and delete cookies for a domain in webbrowser control without using Javascript (which doesn't allow to set / delete cookies without navigating to the website first.)

like image 975
dr. evil Avatar asked Nov 06 '09 17:11

dr. evil


People also ask

How is cookie domain set?

The domain of a cookie is set by the server via the Set-Cookie header and not by the user-agent (browser). In javascript, setting cookies to foreign domains are silently ignored.

What is WebBrowser control?

The WebBrowser control provides a managed wrapper for the WebBrowser ActiveX control. The managed wrapper lets you display Web pages in your Windows Forms client applications.


2 Answers

Managed to accomplish this task by combining these 2:

http://support.microsoft.com/kb/815718

and INTERNET_OPTION_END_BROWSER_SESSION - http://msdn.microsoft.com/en-us/library/windows/desktop/aa385328(v=vs.85).aspx

like image 133
dr. evil Avatar answered Nov 15 '22 20:11

dr. evil


Hope this helps

using System.Runtime.InteropServices;

namespace Storm8
{
    class Program
    {

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

        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool InternetGetCookie(
            string lpszUrlName,
            string lpszCookieName,
            StringBuilder lpszCookieData,
            [MarshalAs(UnmanagedType.U4)]
            ref int lpdwSize
        );

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

        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool InternetSetOption(
            int hInternet,
            int dwOption,
            string lpBuffer,
            int dwBufferLength
        );



        [STAThread]
        static void Main(string[] args)
        {
            InternetSetOption(0, 42, null, 0);
            InternetSetCookie("http://domain.name.com", "cookiename", "cookievalue");

            WebBrowser wb = new WebBrowser();
            string testUrl = "http://domain.name.com/fight.php?showAttackBg=true";
            string additionalHeaders = "User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit /528.18 (KHTML, like Gecko) Mobile/7A341" + Environment.NewLine +
                "Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + Environment.NewLine +
                "Accept-Language: en-gb";

            if (wb.Document == null)
                wb.Navigate(testUrl, null, null, additionalHeaders);

            while (wb.Document == null)
                Application.DoEvents();

            Console.WriteLine("\nPress any key to continue...");
            Console.ReadKey(true);
        }
    }
}

Reference

like image 22
Michalis Avatar answered Nov 15 '22 19:11

Michalis