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
}

14 day free trial Cookiebot

Cookie scanner, cookie banner, cookie declaration and cookie consent in one solution.

  • Use cookies on your website compliant with GDPR, ePrivacy and cookie legislation
  • Cookie management completely automated
  • Cookie banner based on your corporate identity
  • Automatic composed cookie declaration, always up to date

The Cookiebot solution runs on 1.4 million websites, manages 5.2 billion monthly User Consents and supports 47+ languages.