Server side usage – C#

Server side usage – C#

If your website is setting cookies server side, you can determine what type of cookies you are allowed to set by checking the users current consent state through standard cookie handling.

C#

using System.Web.Script.Serialization;
...

HttpCookie CurrentUserConsent = Request.Cookies["CookieConsent"];

if (CurrentUserConsent != null)
{
    switch (CurrentUserConsent.Value)
    {
        case "0":
            //The user has not accepted cookies - set strictly necessary cookies only
            break;

        case "-1":
            //The user is not within a region that requires consent - all cookies are accepted
            break;

        default: //The user has accepted one or more type of cookies

            //Read current user consent in encoded JavaScript format
            JavaScriptSerializer CookieConsentSerializer = new JavaScriptSerializer();
            dynamic CookieConsent = CookieConsentSerializer.Deserialize<object>(HttpUtility.UrlDecode(CurrentUserConsent.Value));

            if (CookieConsent["preferences"])
            {
                //Current user accepts preference cookies
            }
            else
            {
                //Current user does NOT accept preference cookies
            }

            if (CookieConsent["statistics"])
            {
                //Current user accepts statistics cookies
            }
            else
            {
                //Current user does NOT accept statistics cookies
            }

            if (CookieConsent["marketing"])
            {
                //Current user accepts marketing cookies
            }
            else
            {
                //Current user does NOT accept marketing cookies
            }   
    }
}
else
{
    //The user has not accepted cookies - set strictly necessary cookies only
}

Probeer nu 14 dagen gratis Cookiebot

Cookie scanner, cookie banner, cookieverklaring en cookie toestemming in één.

  • Cookies op je website gebruiken volgens AVG, ePrivacy en cookie wetgeving
  • Cookiebeheer volledig geautomatiseerd
  • Cookie banner op basis van jouw huisstijl
  • Automatisch opgestelde cookieverklaring, altijd up to date

De Cookiebot oplossing draait op 1.4 miljoen Websites, beheert 5.2 miljard maandelijkse User Consents en Ondersteunt 47+ talen.