0

I'm working in .Net Framework 4.8 for work. We have a cookie banner in an ascx file that appears throughout our site.

I've tried adding the cookie two ways:

with JS

  function setCookie() {
    var date = new Date();
    var expiration;
    var cookieString
    if (<%=i_ConsentMonths%> > 0) {
      expiration = addMonths(<%=i_ConsentMonths%>, date);
      cookieString = "<%=i_CookieName%>" + "=true;" + "expires=" + expiration.toUTCString() + ";";
    } else {
      cookieString = "<%=i_CookieName%>" + "=true" + ";";
    }
    document.cookie = cookieString;
    var cookieBanner = jQuery("#CookieConsentBanner");
    cookieBanner.hide(100);
  }

  function addMonths(numOfMonths, date = new Date()) {
    date.setMonth(date.getMonth() + numOfMonths);

    return date;
  }

with .Net

  Sub Handle_BannerClick(ByVal sender As Object, ByVal e As EventArgs)
    If sender Is agreeButton Then
      ' Set cookie to indicate consent given
      If i_ConsentCookie Is Nothing Then i_ConsentCookie = New HttpCookie(i_CookieName, "true")
      Response.Cookies.Add(i_ConsentCookie)
      i_CookieWasAdded = True
      ' If finite consent length is required then set cookie expiry date
      If i_ConsentMonths > 0 Then i_ConsentCookie.Expires = Now.AddMonths(i_ConsentMonths)
    ElseIf sender Is closeButton Then
      ' Set session variable to hide banner
      Session("CookieConsentHidden") = True
    End If
  End Sub

In both cases, the cookie gets added fine and users can continue what they're doing, but on the next postback any information stored in the ViewState disappears and causes a crash. the SessionState is fine. Only the ViewState is affected. My team is stumped. Does anyone have any ideas?

0 Answers0