Class CookieManager
- Namespace
- Dynamicweb.Environment
- Assembly
- Dynamicweb.Core.dll
Provides methods to work with cookies
public class CookieManager
- Inheritance
-
CookieManager
- Inherited Members
Properties
CookieOptInLevelExists
Returns true if opt-in level cookie was already set, otherwise false.
public static bool CookieOptInLevelExists { get; }
Property Value
IsCookieManagementActive
Gets a value indicating whether cookie management is active.
public static bool IsCookieManagementActive { get; }
Property Value
- bool
true
if cookie management is active; otherwise,false
.
Methods
AddCategory(string)
Adds the category.
public static void AddCategory(string category)
Parameters
category
stringThe category.
CanCookieBeSet(Cookie)
Returns true if cookie can be set, otherwise false. Cookie cannot be set if cookie opt-in level is None or cookie opt-in level is Functional and cookie type is not Functional.
public static bool CanCookieBeSet(Cookie cookie)
Parameters
cookie
CookieCookie to check if it can be set.
Returns
- bool
true
if cookie can be set, otherwisefalse
.
ClearCache()
Clears the cache.
public static void ClearCache()
ExpireCookie(string)
Tries to expire the specified cookie.
public static bool ExpireCookie(string cookieName)
Parameters
cookieName
stringName of cookie
Returns
- bool
wasExpired
GetAllCategoryAndTrackingCookies()
Gets both category cookies and tracking cookies.
public static IDictionary<string, ICollection<string>> GetAllCategoryAndTrackingCookies()
Returns
GetAllCookieCategories()
Gets list of cookie categories including tracking.
public static ICollection<string> GetAllCookieCategories()
Returns
GetCategories()
Gets the categories.
public static List<string> GetCategories()
Returns
GetCategoryCookies(string)
Gets the category cookies.
public static List<string> GetCategoryCookies(string category)
Parameters
category
stringThe category.
Returns
GetCookie(string)
Gets the cookie with the given name.
public static Cookie? GetCookie(string name)
Parameters
name
stringThe name of the cookie to get.
Returns
GetCookieOptInCategories()
Gets the cookie opt in categories.
public static List<string> GetCookieOptInCategories()
Returns
GetCookieOptInLevel()
Gets opt-in level cookie value. If opt-in level cookie doesn't exists returns Functional cookie opt-in level by default.
public static CookieOptInLevel GetCookieOptInLevel()
Returns
- CookieOptInLevel
Cookie opt-in level
Examples
public void GetCookieOptInLevel()
{
// Get the Opt-In Level cookie value
// If Opt-In Level has not been set, this will return "CookieOptInLevel.Functional"
CookieOptInLevel level = CookieManager.GetCookieOptInLevel();
}
Remarks
Functional cookie opt-in level is default
GetCookieType(Cookie)
Gets cookie type: Tracking or Functional
public static CookieType GetCookieType(Cookie cookie)
Parameters
cookie
CookieCookie to get it's type.
Returns
- CookieType
Cookie type
GetDynamicwebCookiesList()
Represents dynamicweb default cookies list with exclusive cookies list
public static List<string> GetDynamicwebCookiesList()
Returns
GetTrackingCookies()
Gets the tracking cookies.
public static ICollection<string> GetTrackingCookies()
Returns
RemoveCategory(string)
Removes the category.
public static void RemoveCategory(string category)
Parameters
category
stringThe category.
RemoveExistingCookies()
Removes existing cookies from the browser
public static void RemoveExistingCookies()
SetCategoryCookies(string, IEnumerable<string>?)
Sets the category cookies.
public static void SetCategoryCookies(string category, IEnumerable<string>? cookies)
Parameters
category
stringThe category.
cookies
IEnumerable<string>The cookies.
SetCookie(Cookie)
Request a cookie to be set. If cookies are disabled or if the user has opted out of this particular type of cookie, the cookie will not be set. Cookie type can be assigned from the Management Center.
public static Cookie? SetCookie(Cookie cookie)
Parameters
cookie
CookieThe cookie to set.
Returns
Examples
public void SetCookie()
{
// 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.
if (CookieManager.SetCookie(myCookie) != null)
Context.Current.Response.Write("Cookie set successfully!");
else
Context.Current.Response.Write("Cookie not set successfully!");
}
SetCookie(string, string, DateTime)
Request a cookie to be set. If cookies are disabled or if the user has opted out of this particular type of cookie, the cookie will not be set.
public static Cookie? SetCookie(string name, string value, DateTime expirationDate)
Parameters
name
stringThe name of the cookie to set.
value
stringThe value to set in the cookie.
expirationDate
DateTimeThe expiration time for the cookie.
Returns
Examples
public void SetCookieWithExpiration()
{
// 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));
if (cookie != null)
Context.Current.Response.Write("Cookie set successfully!");
else
Context.Current.Response.Write("Cookie not set successfully!");
}
SetCookieOptIn(CookieOptInLevel, IEnumerable<string>?)
Sets the cookie opt-in.
public static Cookie? SetCookieOptIn(CookieOptInLevel optInLevel, IEnumerable<string>? optInCategories)
Parameters
optInLevel
CookieOptInLevelThe opt-in level.
optInCategories
IEnumerable<string>The opt-in categories.
Returns
SetCookieOptInLevel(CookieOptInLevel)
Set opt-in level cookie
public static Cookie? SetCookieOptInLevel(CookieOptInLevel optInLevel)
Parameters
optInLevel
CookieOptInLevelCookie opt-in level
Returns
- Cookie
null
Context.Current.Response
==null
; otherwise,CookieOptInLevel
.
Examples
public 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);
if (optInLevelCookie != null)
{
Context.Current.Response.Write("Opt-In Level Cookie was set successfully!");
}
else
{
// Something went wrong
// HttpContext.Current or HttpContext.Current.Response is not available
Context.Current.Response.Write("Opt-In Level Cookie was not set successfully!");
}
}
SetTrackingCookies(IEnumerable<string>?)
Sets the tracking cookies.
public static void SetTrackingCookies(IEnumerable<string>? cookies)
Parameters
cookies
IEnumerable<string>The cookies.
UpdateCookie(Cookie)
Update cookie. If cookies are disabled or if the user has opted out of this particular type of cookie, the cookie will not be updated. Cookie type can be assigned from the Management Center.
public static Cookie? UpdateCookie(Cookie cookie)
Parameters
cookie
CookieThe cookie to update.
Returns
Examples
public 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) != null)
{
testCookie = new Cookie("SampleOtherUpdateCookie", "Color=White&Size=36");
testCookie.Expires = System.DateTime.Now.AddDays(2);
Cookie updatedTestCookie = CookieManager.UpdateCookie(testCookie);
if (updatedTestCookie != null)
{
Context.Current.Response.Write("Cookie was updated successfully!");
}
}
}
UpdateCookie(string, string, DateTime)
Update cookie. If cookies are disabled or if the user has opted out of this particular type of cookie, the cookie will not be updated. Cookie type can be assigned from the Management Center.
public static Cookie? UpdateCookie(string name, string value, DateTime expirationDate)
Parameters
name
stringThe name of the cookie to set.
value
stringThe value to set in the cookie.
expirationDate
DateTimeThe expiration time for the cookie.
Returns
Examples
public 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));
if (updatedCookie != null)
{
Context.Current.Response.Write("Cookie was updated successfully!");
}
}
UpdateTrackingCookiesList()
Populate tracking cookies list from global settings
public static void UpdateTrackingCookiesList()