asp.net-mvcasp.net-coregdprconsentformcookieconsent

(GDPR) Asp.net core ITrackingConsentFeature not working properly with cookie


I'm using asp.net core MVC with TargetFramework net5.0.

I'm implementing EU General Data Protection Regulation (GDPR) support in ASP.NET Core.

I exactly implemented this article https://learn.microsoft.com/en-us/aspnet/core/security/gdpr?view=aspnetcore-3.1.

But when I set document.cookie by cookieString then nothing happened.

I figured out this issue is related to cookieString that generated by consentFeature?.CreateConsentCookie().

Because the separation here is ; so I change that with .Replace(";", ",").

It's work now but I faced an issue with expiration DateTime. It's not working anymore. It's equal to When the browsing session ends

Do you have any suggestions?

Thanks,


Solution

  • Finally, I found another answer to that.

    The problem in related to cookieString when I want to add in to the document.cookie.

    @{
        var consentFeature = Context.Features.Get<ITrackingConsentFeature>();
        var showBanner = !consentFeature?.CanTrack ?? false;
        var cookieString = consentFeature?.CreateConsentCookie();
    }
    

    I just a added cookieString as a string with the end of double quation end of the string.

    document.cookie = `${button.dataset.cookieString}"`;
    

    I don't know It's a good way or not but It works.