Posted on 26/05/2025 15:58:54
Hi Andreas
Cookiemanager.SetCookie will only run if the below things are true. So first of all, check that cookie manager is active. Next check if you have opted into cookies in DW cookie banner - if you have not implemented DW cookie banner, you cannot accept and hence you cannot set the cookie - cookie law things.
If you really do not care about cookie laws, like most people, you can just set the cookie like this:
//Create cookie
Cookie cookie = new Cookie(name, value);
cookie.Expires = expirationDate;
Context.Current?.Response.SetCookie(cookie);
Validation of cookies using the cookiemanager:
public static bool CanCookieBeSet(Cookie cookie)
{
if (ExecutingContext.IsBackEnd())
{
return true;
}
if (!IsCookieManagementActive)
{
//Allow cookies if Cookie Management is disabled.
return true;
}
if (GetCookieOptInLevel() == CookieOptInLevel.None)
{
//Disallow all cookies if CookieOptInLevel.None.
return false;
}
if (GetCookieOptInLevel() == CookieOptInLevel.All)
{
//Allow all if CookieOptInLevel.All.
return true;
}
//At least CookieOptInLevel.Functional here, so allow CookieType.Functional
var canBeSet = GetCookieType(cookie) == CookieType.Functional;
if (canBeSet)
{
// If the cookie is included in any custom categories then check if opt-in exists for those categories
canBeSet = HasCookieOptInForAllCategories(cookie);
}
return canBeSet;
}