Cefsharp clearing cache,cookies and browser data in wpf
Asked Answered
H

2

10

I am developing an app on a banking device that uses cefsharp browser as a part of it. Cause this app will be used by anyone it should not save any data from previous user that the next user can see. I want to clear all cached browser data after closing it.

public void InitBrowser()
    {
        settings = new CefSettings();
        settings.CachePath = AppDomain.CurrentDomain.BaseDirectory + "cache";
        settings.CefCommandLineArgs.Add("disable-application-cache", "1");
        settings.CefCommandLineArgs.Add("disable-session-storage", "1");
        if (!Cef.IsInitialized) Cef.Initialize(settings);
        webBrowser = new CefSharp.Wpf.ChromiumWebBrowser();
        MainGrid.Children.Add(webBrowser);
    }

I want to clear all cached data after a function named WebPages_Exit is called. How can I remove all cached data without removing the browser instance or shutting down the CEF cause CEF can't be initialized twice and creating another instance of browser after disposing it is not working.

I implemented visit function in ICookieVisitor to save cookies as well and used methods like deleteCookies or disabling cache cefSetting command, but nothing works cause cookies list is empty and visit function of IcookieVisitor is never called. it seems that it is saved in another part and just resets when CEF will shutdown.

Hijoung answered 10/12, 2017 at 14:12 Comment(0)
H
19

I found the answer! It was because of disabling cache setting. By doing so, it actually caches the data but it won't be accessible. For example, you can't remove cookies without shutting down CEF. So, if you enable cache setting (leave it as the default), you can remove them with Cef.GetGlobalCookieManager().DeleteCookies("", "").

Hijoung answered 10/12, 2017 at 16:33 Comment(2)
DeleteCookies() will just delete cookies, not cached data such as css, js, etc. For that I think you need to use Browser.GetBrowser.Reload(True)Amphiboly
Note that Cef.GetGlobalCookieManager() can return null depending on some conditions.Reclaim
P
2

You can delete cache folders before calling Cef.Initialize:

Directory.Delete(cacheDirBase + "\\blob_storage", true);
Directory.Delete(cacheDirBase + "\\Cache", true);
Directory.Delete(cacheDirBase + "\\Code Cache", true);
Directory.Delete(cacheDirBase + "\\GPUCache", true);
Directory.Delete(cacheDirBase + "\\Local Storage", true);
Directory.Delete(cacheDirBase + "\\Session Storage", true);

Just remove whichever you don't want to delete.

Pipes answered 21/10, 2020 at 6:36 Comment(2)
What is the cacheDirBase?Transfuse
You can find the cache folders here: C:\Users\<username>\AppData\Local\CefSharp\Jacquerie

© 2022 - 2024 — McMap. All rights reserved.