Table of Contents

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

bool

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 string

The category.

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 Cookie

Cookie to check if it can be set.

Returns

bool

true if cookie can be set, otherwise false.

ClearCache()

Clears the cache.

public static void ClearCache()

ExpireCookie(string)

Tries to expire the specified cookie.

public static bool ExpireCookie(string cookieName)

Parameters

cookieName string

Name of cookie

Returns

bool

wasExpired

GetAllCategoryAndTrackingCookies()

Gets both category cookies and tracking cookies.

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

Returns

IDictionary<string, ICollection<string>>

GetAllCookieCategories()

Gets list of cookie categories including tracking.

public static ICollection<string> GetAllCookieCategories()

Returns

ICollection<string>

GetCategories()

Gets the categories.

public static List<string> GetCategories()

Returns

List<string>

GetCategoryCookies(string)

Gets the category cookies.

public static List<string> GetCategoryCookies(string category)

Parameters

category string

The category.

Returns

List<string>

GetCookie(string)

Gets the cookie with the given name.

public static Cookie? GetCookie(string name)

Parameters

name string

The name of the cookie to get.

Returns

Cookie

Returns the cookie with the given name.

GetCookieOptInCategories()

Gets the cookie opt in categories.

public static List<string> GetCookieOptInCategories()

Returns

List<string>

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

Gets cookie type: Tracking or Functional

public static CookieType GetCookieType(Cookie cookie)

Parameters

cookie Cookie

Cookie 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

List<string>

Dynamicweb default cookies

GetTrackingCookies()

Gets the tracking cookies.

public static ICollection<string> GetTrackingCookies()

Returns

ICollection<string>

RemoveCategory(string)

Removes the category.

public static void RemoveCategory(string category)

Parameters

category string

The 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 string

The category.

cookies IEnumerable<string>

The cookies.

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 Cookie

The cookie to set.

Returns

Cookie

If allowed return the cookie that is being set; otherwise null.

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 string

The name of the cookie to set.

value string

The value to set in the cookie.

expirationDate DateTime

The expiration time for the cookie.

Returns

Cookie

If allowed return the cookie that is being set; otherwise null.

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 CookieOptInLevel

The opt-in level.

optInCategories IEnumerable<string>

The opt-in categories.

Returns

Cookie

SetCookieOptInLevel(CookieOptInLevel)

Set opt-in level cookie

public static Cookie? SetCookieOptInLevel(CookieOptInLevel optInLevel)

Parameters

optInLevel CookieOptInLevel

Cookie 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.

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 Cookie

The cookie to update.

Returns

Cookie

If allowed returns the updated cookie; otherwise null.

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 string

The name of the cookie to set.

value string

The value to set in the cookie.

expirationDate DateTime

The expiration time for the cookie.

Returns

Cookie

If allowed returns the updated cookie; otherwise null.

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()
To top