Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate cache per browser?

Currently I'm setting the cache path as follows:

CefSettings settings = new CefSettings();
settings.CachePath = mycachePath;

Cef.Initialize(settings);

var browser = new ChromiumWebBrowser(myUrl);

The above works.

However, I need to login to a website with 2 different accounts simultaneously but it uses the same cookie container. So if I login with one account and then the other, the first account is overridden.

Is it possible to have a have a cache path per browser?

Or is there a better way to handle this situation?

like image 375
Ivan-Mark Debono Avatar asked Dec 31 '15 17:12

Ivan-Mark Debono


People also ask

Does each browser have its own cache?

Each browser application (Internet Explorer, Google Chrome, Mozilla Firefox, Apple Safari) maintains its own set of “cached” files and cookies. Each browser's cache may have many thousands of files. Each browser's cookie store may have scores of cookies.

Can I clear cache just for one website?

Here's how to clear the browser cache for just one web page: Open the webpage you want to clear the cache for, and go into Chrome Developer Tools. That's CMD+Option+I on a Mac, and CTRL+Shift+I or F12 on Windows, Linux, and Chromebooks. Click Empty Cache and Hard Reload in the list of options, and you're done.

Can I clear cache for just one website safari?

Launch Safari browser on your Mac. Click the Privacy tab and select Manage Website Data.... Select a website that is listed, then click Remove.

How do I clear cache in multiple browsers?

Click the Tools menu (three dotted lines in the upper-right corner), and open the Settings menu. Click Privacy, search, and services on the left-side menu. Under the section Clear browsing data, click Choose what to clear. Select Cookies and other site data and Cached images and files.


2 Answers

It looks like you're using CefSharp? If so, looking through the code, it seems that you want to create the browser with an empty CachePath:

/// <summary>
/// Returns the cache path for this object. If empty an "incognito mode"
/// in-memory cache is being used.
/// </summary>
string CachePath { get; }

Looking at their sample (I'm assuming windowless), this looks like it'll get roughly what you want:

var browserSettings = new BrowserSettings();
var requestContextSettings = new RequestContextSettings { CachePath = "" };

using(var requestContext = new RequestContext(requestContextSettings))
using (var browser = new ChromiumWebBrowser(TestUrl, browserSettings, requestContext))
{
    ...
}
like image 95
Prescott Avatar answered Oct 04 '22 19:10

Prescott


This is old, but I just came across it and it needs a more complete answer. You can have as many browser instances open as you want, each with its own separate cache & cookies that are independent of the others. All you have to do is set the CachePath settings property for each browser, making sure its path is distinct, and then create the browser.

An example scenario in which you might use this is with tabs, where Tab1 has Browser1, Tab2 has Browser2, etc. and each browser instance has no knowledge of the others. That is achieved by giving each browser its own cache path before creating it.

In VB .NET:

        CEFPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\My\Special\Cache\Path"

        If Not Directory.Exists(CEFPath) Then
            Try
                Directory.CreateDirectory(CEFPath)
            Catch ex As Exception
                MsgBox("Error creating cache directory" + vbCrLf + CEFPath,, "Error")
            End Try
        End If

        Dim settings As New CefSettings()
        settings.CachePath = CEFPath

        'Settings.Proxy = new ProxyOptions(ip: "myipaddress", port: "myport", username: "myusername", password: "mypassword")

        ' initialization before creating instance
        If CefSharp.Cef.IsInitialized = False Then
            CefSharp.Cef.Initialize(settings)
        End If

        browser = New ChromiumWebBrowser("")

        Dim requestContextSettings As New RequestContextSettings()
        requestContextSettings.CachePath = CEFPath
        'Optional:
        requestContextSettings.PersistSessionCookies = True

        'https://github.com/cefsharp/CefSharp/wiki/General-Usage
        browser.RequestContext = New RequestContext(requestContextSettings)

I am using NuGet packages v83.4.20

like image 45
technonaut Avatar answered Oct 04 '22 20:10

technonaut