M
M
Maxim Anarchistov2015-07-10 19:08:48
.NET
Maxim Anarchistov, 2015-07-10 19:08:48

C# How to clear all cookies in WebBrowser?

There are options to switch the browser by calling the cleanup javascript (the parser ate the code), but this code only works for one specific site and then every other time.
There is also an option to clean the directory

string Path = Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
            try
            {
                System.IO.Directory.Delete(Path, true);
            }
            catch (Exception)
            {
            }

but this option only works after restarting the application. There is also a workable option.
Process.Start("cmd.exe", "/C RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255");

but running cmd in the background for the sake of clearing cookies is a bummer.
Is there any more or less adequate way so that you do not have to go through the troubles as described above?
UPD: This method works: mdb-blog.blogspot.ru/2013/02/c-winforms-webbrowser...
But unsafe code must be allowed for this method.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Maxim Anarchistov, 2016-12-21
@ozonar

Indeed, the line deleted cookies:
browser.Document.Cookie = name +"=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
which added another relatively pointless way to my treasury of silly ways to delete cookies.
Hm.

M
mayorovp, 2015-07-10
@mayorovp

WebBrowser is Internet Explorer

S
Stanislav Makarov, 2015-07-10
@Nipheris

Try setting this property to null or an empty string.

N
nequtr, 2016-02-25
@nequtr

all nonsense, here is the working code for VK, i.e. api functions are 100% reliable, the downside is that you need to understand how to work with InternetGetCookie:
InternetSetCookie(" m.vk.com ", "remixsid", ""); //for example, clear authorization in VK
for specifically for each site, I think it's easy to guess
try{
foreach (Cookie c in Core.ActClient.CookieContainer.GetCookies(new Uri(" m.vk.com "))){
InternetSetCookie(" m.vk .com ",c.Name, "");
}
}catch{}
[DllImport("wininet.dll", CharSet = CharSet.Auto,
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
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question