Each CookieStorage stores cookies on a local file system in Chromium's user data directory. So, each CookieStorage depends on BrowserContext that defines where Chromium's user data folder is located. If you need to use different cookie storages for different Browser instances, then you need to configure Browser instances with different BrowserContext instances and different Chromium's user data directories. Browser instances with same BrowserContext instance will share the cookies.


The following code demonstrates how to create two Browser instances that don't share cookies:


BrowserContextParams params1= new BrowserContextParams("C:\\my-data1");
BrowserContext context1 = new BrowserContext(params1);
Browser browser1 = BrowserFactory.Create(context1);

BrowserContextParams params2= new BrowserContextParams("C:\\my-data2");
BrowserContext context2 = new BrowserContext(params2);
Browser browser2 = BrowserFactory.Create(context2);


To access CookieStorage of Browser instance use the browser.CookieStorage property. Using CookieStorage you can get, modify, and delete cookies. When you modify cookies you must save changes using the CookieStorage.Save() method. For example:


CookieStorage cookieStorage = browser.CookieStorage;
cookieStorage.SetSessionCookie(
    "http://www.a.com", "name1", "value1", ".a.com", "/", false, false);
cookieStorage.Save();


DotNetBrowser supports cookies. Cookies are stored in Chromium user data directory (persistent cookies) and application memory (session cookies). If you delete Chromium user data directory, then all persistent cookies will be removed. Session cookies will be removed automatically when corresponding application terminates. Apart from persistent and session cookies, DotNetBrowser supports secure and http-only cookies.


Getting All Cookies

List<Cookie> cookies = cookieStorage.GetAllCookies();
foreach (Cookie cookie in cookies) {
    Console.WriteLine("cookie = " + cookie);
}


Getting All Cookies by URL

List<Cookie> cookies = cookieStorage.GetAllCookies("http://www.google.com");
foreach (Cookie cookie in cookies) {
    Console.WriteLine("cookie = " + cookie);
}


Creating a Cookie

// Create and add new cookie
int microsecondsOffset = 1000;
double expires = (DateTime.Now.AddHours(1) - DateTime.MinValue).TotalMilliseconds;
// Cookie will be alive during one hour starting from now
long expirationTimeInMicroseconds = (long)expires * microsecondsOffset;
cookieStorage.SetCookie("http://www.google.com", 
    "mycookie", "myvalue", ".google.com", 
  "/", expirationTimeInMicroseconds, false, false);
cookieStorage.Save();


Creating a Session Cookie

// Create and add new session cookie
cookieStorage.SetSessionCookie("http://www.google.com", 
    "mycookie", "myvalue", ".google.com", "/", false, false);
cookieStorage.Save();


Deleting All Cookies

int numberOfDeletedCookies = cookieStorage.DeleteAll();
cookieStorage.Save();


Deleting a Cookie

List<Cookie> cookies = cookieStorage.GetAllCookies();
foreach (Cookie cookie in cookies) {
    bool success = cookieStorage.Delete(cookie);    
}
cookieStorage.Save();