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 webs
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.
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
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).
Here is a blog post on how do delete cookies in WebBrowser control using wininet.
http://www.alphatecit.com.au/code-snippets/facebook-c-sdk-multiple-login-problem-resolved/
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
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