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
}