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:

C#

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);

VB.NET

Dim params1 As BrowserContextParams = New BrowserContextParams("C:\\my-data1")
Dim context1 As BrowserContext = New BrowserContext(params1)
Dim browser1 As Browser = BrowserFactory.Create(context1)

Dim params2 As BrowserContextParams = New BrowserContextParams("C:\\my-data2")
Dim context2 As BrowserContext = New BrowserContext(params2)
Dim browser2 As Browser = 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:

C#

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

VB.NET

Dim cookieStorage As 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

C#

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

VB.NET

Dim cookies As List(Of Cookie) = cookieStorage.GetAllCookies()
For Each cookie As Cookie In cookies
    Console.WriteLine("cookie = " + cookie.ToString())
Next


Getting All Cookies by URL

C#

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

VB.NET

Dim cookies As List(Of Cookie) = cookieStorage.GetAllCookies("http://www.google.com")
For Each cookie As Cookie In cookies
    Console.WriteLine("cookie = " + cookie.ToString())
Next


Creating a Cookie

C#

// 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();

VB.NET

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


Creating a Session Cookie

C#

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

VB.NET

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


Deleting All Cookies

C#

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

VB.NET

Dim numberOfDeletedCookies As Integer = cookieStorage.DeleteAll()
cookieStorage.Save()


Deleting a Cookie

C#

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

VB.NET

Dim cookies As List(Of Cookie) = cookieStorage.GetAllCookies()
For Each cookie As Cookie In cookies
    Dim success As Boolean = cookieStorage.Delete(cookie)
Next
cookieStorage.Save()