12

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.)

dr. evil
  • 26,944
  • 33
  • 131
  • 201
  • What is your requirement/scenario for this? "From webbrowser control" might not be necessary. Do you have control of the site? If you do, you can add and remove cookies from the HttpRequest, and that API allows you to specify the cookie domain. – Merlyn Morgan-Graham Aug 20 '11 at 08:51
  • I have a browser and I want to add the ability to modify all the cookies for a given website. – dr. evil Aug 20 '11 at 15:09

6 Answers6

2

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

sinelaw
  • 16,205
  • 3
  • 49
  • 80
Michalis
  • 1,508
  • 1
  • 10
  • 10
2

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

dr. evil
  • 26,944
  • 33
  • 131
  • 201
  • 2
    I have gone through the URLs but i did not find any solution. can you please give me the brief solution. I got struck with same problem from last 10 days – Avinash Aug 02 '13 at 10:13
  • 2
    @dr.evil Can you please provide a summary about what you did to resolve this?... Link only answers are not so good in SO. – LCJ Apr 04 '14 at 20:57
0

IE Uses WinInet functions for networking so you can use WinInet's cookie functions to change the cookie. Update: The requirement demands per-process setting. Since the cache folder location is not stored in IE settings registry key IDocHostUIHandler2::GetOverrideKeyPath won't work. I don't know a way to customize the cookie folder location at the process level except to hook all WinInet APIs (and stuck with updating application to accommodate future WinInet APIs).

Sheng Jiang 蒋晟
  • 15,125
  • 2
  • 28
  • 46
  • 1
    Isn't that for whole IE? I don't want to delete users' browser cookies for IE, it should be something only for the current application's hosted web control. Correct me if I'm wrong. – dr. evil Nov 07 '09 at 16:38
  • If you want to isolate your application's cookies you need to override the Cache directory registry setting via IDocHostUIHandler2::GetOverrideKeyPath – Sheng Jiang 蒋晟 Nov 08 '09 at 01:27
  • I'm not positive that the GetOverrideKeyPath trick would even work. You'll want to use InternetSetCookieEx either way. – EricLaw Nov 12 '09 at 02:20
  • Yes, you are right, the IE cache folder setting isn't under IE settings. It is under shell folder settings. – Sheng Jiang 蒋晟 Nov 12 '09 at 18:09
0

Here's a finer solution that only clears cookies (C/C++):

#include <wininet.h>
#include <winineti.h>
...
DWORD dwSuppress = INTERNET_SUPPRESS_COOKIE_PERSIST;
InternetSetOption(0, INTERNET_OPTION_SUPPRESS_BEHAVIOR, &dwSuppress, sizeof(DWORD));

All credits to this blog post (C#). Don't forget to check the documentation for InternetSetOption and INTERNET_SUPPRESS_COOKIE_PERSIST

Gyum Fox
  • 3,287
  • 2
  • 41
  • 71
  • It does not clear cookies. It does something else. During your process, normally persistent cookies will not be persisted. But cookies that are already there, already stored, are not affected at all. Just try to run and view the actual list of cookies in Internet Options. – Gábor Apr 05 '18 at 13:33
-1

Here is a blog post on how do delete cookies in WebBrowser control using wininet.

http://web.archive.org/web/20140219074039/http://www.alphatecit.com.au/code-snippets/facebook-c-sdk-multiple-login-problem-resolved

i_saw_drones
  • 3,486
  • 1
  • 31
  • 50
prabir
  • 7,674
  • 4
  • 31
  • 43
  • The link is also now dead. Please include a bare bones solution when posting an external link. – j b Mar 24 '17 at 15:22
-4

You can't delete cookies for a domain other than the domain of the current site.

To do what you are asking requires you have access to the machine (i.e. toolbar installed). Even then it's kludgy.

The only exception is if you are on a domain where the cookie is using the * wildcard, for example *.stackoverflow.com. If you change a cookie with the wildcard, then all the child sub domains (i.e. blog.stackoverflow.com) will have access and see the change that was made to the cookie.

Chuck Conway
  • 16,287
  • 11
  • 58
  • 101