Table of Contents

Class CookieManager

Namespace
Dynamicweb.Environment
Assembly
Dynamicweb.Core.dll

Provides methods to work with cookies including:

  • Opt-in management for functional vs tracking cookies
  • Categorization of custom cookie groups
  • Centralized routines for setting, updating, expiring and removing cookies
public class CookieManager
Inheritance
CookieManager
Inherited Members

Properties

CookieOptInLevelExists

Returns true if the opt-in level cookie already exists.

public static bool CookieOptInLevelExists { get; }

Property Value

bool

IsCookieManagementActive

Gets a value indicating whether cookie management (consent-based restriction of tracking cookies) is active.

public static bool IsCookieManagementActive { get; }

Property Value

bool

Methods

AddCategory(string)

Adds a new cookie category to configuration (if not empty and not already present).

public static void AddCategory(string category)

Parameters

category string

Category name to add.

Determines whether a cookie can be set given current opt-in level and category consents.

public static bool CanCookieBeSet(Cookie cookie)

Parameters

cookie Cookie

Cookie to evaluate.

Returns

bool

True if allowed; otherwise false.

ClearCache()

Clears all cached cookie data to force reload from configuration on next access.

public static void ClearCache()

ExpireCookie(string)

Expires a cookie by name if present, clearing its values, and sets an immediate past expiration.

public static bool ExpireCookie(string cookieName)

Parameters

cookieName string

Cookie name to expire.

Returns

bool

True if found and expired; otherwise false.

GetAllCategoryAndTrackingCookies()

Builds a dictionary mapping each category (including tracking) to its cookies.

public static IDictionary<string, ICollection<string>> GetAllCategoryAndTrackingCookies()

Returns

IDictionary<string, ICollection<string>>

Dictionary of category => cookie collection.

GetAllCookieCategories()

Gets all available cookie categories including the special "Tracking" pseudo-category.

public static ICollection<string> GetAllCookieCategories()

Returns

ICollection<string>

Collection of all category names including "Tracking".

GetCategories()

Gets the list of configured cookie categories.

public static List<string> GetCategories()

Returns

List<string>

Alphabetically sorted list of category names.

GetCategoryCookies(string)

Gets cookie names associated with a category.

public static List<string> GetCategoryCookies(string category)

Parameters

category string

Category name.

Returns

List<string>

List of cookie names (flattened) or empty list if category undefined or no cookies assigned.

GetCookie(string)

Gets the cookie with the given name from the current request.

public static Cookie? GetCookie(string name)

Parameters

name string

Cookie name.

Returns

Cookie

The cookie or null if not present or context unavailable.

GetCookieOptInCategories()

Gets the category names the user has granted consent for (from opt-in cookie).

public static List<string> GetCookieOptInCategories()

Returns

List<string>

List of category names or empty if none.

GetCookieOptInLevel()

Gets opt-in level cookie value. Returns Functional by default if cookie does not exist or context missing.

public static CookieOptInLevel GetCookieOptInLevel()

Returns

CookieOptInLevel

Current cookie opt-in level.

Gets the cookie type (Functional or Tracking) based on configured tracking cookie names.

public static CookieType GetCookieType(Cookie cookie)

Parameters

cookie Cookie

Cookie to inspect.

Returns

CookieType

Tracking if its name or any sub-value matches tracking configuration; otherwise Functional.

GetDynamicwebCookiesList()

Represents dynamicweb default cookies merged with any exclusively set cookie names for the current lifecycle. Flattened names are returned (cookie or cookie.key).

public static List<string> GetDynamicwebCookiesList()

Returns

List<string>

List of cookie names.

GetTrackingCookies()

Gets tracking cookie names from configuration.

public static ICollection<string> GetTrackingCookies()

Returns

ICollection<string>

Collection of tracking cookie names.

IsCookieOptInGranted(string)

Returns true if opt-in granted to the specified cookie category.

public static bool IsCookieOptInGranted(string optInCategory)

Parameters

optInCategory string

Category to check.

Returns

bool

True if consent exists for this category; otherwise false.

RemoveCategory(string)

Removes the category and its associated cookie list from configuration (if present).

public static void RemoveCategory(string category)

Parameters

category string

Category name to remove.

RemoveExistingCookies()

Iterates over existing request cookies and expires them (browser removal attempt).

public static void RemoveExistingCookies()

SetCategoryCookies(string, IEnumerable<string>?)

Sets the cookies belonging to a given category and persists changes in configuration. Passing null clears the category cookies.

public static void SetCategoryCookies(string category, IEnumerable<string>? cookies)

Parameters

category string

Category name.

cookies IEnumerable<string>

New cookie names or null to clear.

Requests that a cookie be set if allowed by consent and opt-in logic.

public static Cookie? SetCookie(Cookie cookie)

Parameters

cookie Cookie

Cookie to set.

Returns

Cookie

The cookie if set; otherwise null.

public static void SetCookie()
{
    if (Context.Current is null)
        return;

    // Create cookie object
    var myCookie = new Cookie("SomeOtherSampleCookie", "Some other sample value");
    myCookie.Expires = System.DateTime.Now.AddDays(1);

    // Attempt to set cookie.
    // Will return null if the cookie is not allowed by the user.
    Cookie? cookie = CookieManager.SetCookie(myCookie);
}

SetCookie(string, string, DateTime)

Creates and sets a cookie with explicit expiration date if allowed by consent.

public static Cookie? SetCookie(string name, string value, DateTime expirationDate)

Parameters

name string

Cookie name.

value string

Cookie value.

expirationDate DateTime

Expiration date.

Returns

Cookie

The cookie if set; otherwise null.

Examples

public static void SetCookieWithExpiration()
{
    if (Context.Current is null)
        return;

    // Attempt to set cookie.
    // Will return null if the cookie is not allowed by the user.
    Cookie? cookie = CookieManager.SetCookie("SomeSampleCookie", "Some sample value", System.DateTime.Now.AddDays(1));
}

SetCookieOptIn(CookieOptInLevel, IEnumerable<string>?)

Sets the cookie opt-in level and optional category consent cookie.

public static Cookie? SetCookieOptIn(CookieOptInLevel optInLevel, IEnumerable<string>? optInCategories)

Parameters

optInLevel CookieOptInLevel

Opt-in level.

optInCategories IEnumerable<string>

Categories user consented to (optional).

Returns

Cookie

The created cookie or null if response unavailable.

SetCookieOptInLevel(CookieOptInLevel)

Sets the cookie opt-in level without category consent.

public static Cookie? SetCookieOptInLevel(CookieOptInLevel optInLevel)

Parameters

optInLevel CookieOptInLevel

Desired opt-in level.

Returns

Cookie

The created cookie or null if response unavailable.

Examples

public static void SetCookieOptInLevel()
{
    // Set Opt-In Level to allow all cookies
    // Setting the Opt-In will write a cookie containing the current Opt-In Level
    Cookie? optInLevelCookie = CookieManager.SetCookieOptInLevel(CookieOptInLevel.All);
}

SetTrackingCookies(IEnumerable<string>?)

Sets the tracking cookies (flattened names) and persists them in configuration. Passing null clears tracking cookies.

public static void SetTrackingCookies(IEnumerable<string>? cookies)

Parameters

cookies IEnumerable<string>

Tracking cookie names or null.

TryGetCookieDomainNameIfActive(out string?)

public static bool TryGetCookieDomainNameIfActive(out string? domain)

Parameters

domain string

Returns

bool

Updates an existing cookie or sets a new one if not present (if allowed by consent). Merges key/value pairs when cookie has sub-values.

public static Cookie? UpdateCookie(Cookie cookie)

Parameters

cookie Cookie

Cookie carrying updated data.

Returns

Cookie

The updated or newly set cookie; otherwise null.

public static void UpdateCookie()
{
    // Create cookie object
    Cookie testCookie = new Cookie("SampleOtherUpdateCookie", "Color=Red");
    testCookie.Expires = System.DateTime.Now.AddDays(1);

    // Attempt to set cookie.
    // Will return null if the cookie is not allowed by the user.
    if (CookieManager.SetCookie(testCookie) is not null)
    {
        testCookie = new Cookie("SampleOtherUpdateCookie", "Color=White&Size=36");
        testCookie.Expires = System.DateTime.Now.AddDays(2);

        Cookie? updatedTestCookie = CookieManager.UpdateCookie(testCookie);
    }
}

UpdateCookie(string, string, DateTime)

Updates an existing cookie by name with new value and expiration date (if allowed by consent).

public static Cookie? UpdateCookie(string name, string value, DateTime expirationDate)

Parameters

name string

Cookie name.

value string

New cookie value.

expirationDate DateTime

New expiration date.

Returns

Cookie

The updated cookie or null if not allowed.

Examples

public static void UpdateCookieWithExpiration()
{
    // Attempt to set cookie. Will return null if the cookie is not allowed by the user.
    CookieManager.SetCookie("SampleUpdateCookie", "Key1=Value1&Key2=Value2", System.DateTime.Now.AddDays(1));
    //Attempt to update the "SomeSampleCookie" cookie
    Cookie? updatedCookie = CookieManager.UpdateCookie("SampleUpdateCookie", "Key1=UpdatedValue&Key3=Value3", System.DateTime.Now.AddDays(2));
}

UpdateTrackingCookiesList()

Populates the tracking cookies list from global settings.

public static void UpdateTrackingCookiesList()
To top