Developer forum

Forum » CMS - Standard features » Problem with setcookie

Problem with setcookie

Andreas Rundgren
Reply

Hi,

Im trying to use the Setcookie method of the Cookiemanager class.

I do like this:
`CookieManager.SetCookie("qrmsinfo", $"{machineInfo.ObjectNo}|{machineInfo.Description}|{machineInfo.No}|{machineInfo.CustomerProject}|{machineInfo.CustomerNo}|{machineInfo.ManufacturerCode}|{machineInfo.ManufacturerModelCode}", DateTime.Now.AddHours(2));`

But it does not seem to set the cookie, if i try in inkognito it seems to work but not in regular browser (edge, chrome).

Anyone have any clue why my cookie does not set?

Regards
Andreas


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

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;
}

Votes for this answer: 1
 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

See the explanation on the documentation here:

https://doc.dynamicweb.dev/api/Dynamicweb.Environment.CookieManager.html

 
Andreas Rundgren
Reply

Hi 

Thank you for this information. Then i will look into this.

Regards
Andreas

 

You must be logged in to post in the forum