Cefsharp clearing cache,cookies and browser data in wpf

Shervin Rafiee picture Shervin Rafiee · Dec 10, 2017 · Viewed 15.7k times · Source

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.

Answer

Shervin Rafiee picture Shervin Rafiee · Dec 10, 2017

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("", "").