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
IsCookieManagementActive
Gets a value indicating whether cookie management (consent-based restriction of tracking cookies) is active.
public static bool IsCookieManagementActive { get; }
Property Value
Methods
AddCategory(string)
Adds a new cookie category to configuration (if not empty and not already present).
public static void AddCategory(string category)
Parameters
categorystringCategory name to add.
CanCookieBeSet(Cookie)
Determines whether a cookie can be set given current opt-in level and category consents.
public static bool CanCookieBeSet(Cookie cookie)
Parameters
cookieCookieCookie 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
cookieNamestringCookie 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
GetCategoryCookies(string)
Gets cookie names associated with a category.
public static List<string> GetCategoryCookies(string category)
Parameters
categorystringCategory 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
namestringCookie 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
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.
GetCookieType(Cookie)
Gets the cookie type (Functional or Tracking) based on configured tracking cookie names.
public static CookieType GetCookieType(Cookie cookie)
Parameters
cookieCookieCookie 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
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
optInCategorystringCategory 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
categorystringCategory 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
categorystringCategory name.
cookiesIEnumerable<string>New cookie names or null to clear.
SetCookie(Cookie)
Requests that a cookie be set if allowed by consent and opt-in logic.
public static Cookie? SetCookie(Cookie cookie)
Parameters
cookieCookieCookie to set.
Returns
- Cookie
The cookie if set; otherwise null.
Examples
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
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
optInLevelCookieOptInLevelOpt-in level.
optInCategoriesIEnumerable<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
optInLevelCookieOptInLevelDesired 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
cookiesIEnumerable<string>Tracking cookie names or null.
TryGetCookieDomainNameIfActive(out string?)
public static bool TryGetCookieDomainNameIfActive(out string? domain)
Parameters
domainstring
Returns
UpdateCookie(Cookie)
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
cookieCookieCookie carrying updated data.
Returns
- Cookie
The updated or newly set cookie; otherwise null.
Examples
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
namestringCookie name.
valuestringNew cookie value.
expirationDateDateTimeNew 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()