Separate cache per browser?
Asked Answered
F

2

6

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?

Farm answered 31/12, 2015 at 17:18 Comment(4)
If it's possible, you could easily open the second browser in a private sessionFlunk
@cFrozenDeath How do I open the second browser in a private session?Farm
That depends on whether you are opening the browser by hand or programmatically. If the first, right click on Chrome and choose "New incognito window"; if the second, that depends on the class you use and whether it allows you to or notFlunk
@cFrozenDeath I'm doing it in code as shown above.Farm
J
7

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))
{
    ...
}
Jacie answered 31/12, 2015 at 19:50 Comment(1)
I think this answer doesn´t apply to the current version (63.0) because there is no more this Instantiate Method ChromiumWebBrowser(string url, BrowserSettings, RequestContext)Scrawly
G
1

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

Gaffe answered 14/8, 2020 at 20:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.