gehnster / EVEStandard-Examples

Example projects on how to use the EVEStandard library
MIT License
4 stars 2 forks source link

Session storage doesn't work in this example because of GDPR policies #1

Closed MaddoScientisto closed 3 years ago

MaddoScientisto commented 4 years ago

I tried running this example but even after pluggin my application data it would crash on the callback, the reason is that in the startup.cs file the cookie settings are set to require the cookies agreement but the agreement wasn't included in the example page.

The fix is very simple: Add the file _CookieConsentPartial.cshtml to the shared views folder, containing the following:

@using Microsoft.AspNetCore.Http.Features

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

@if (showBanner)
{
    <nav id="cookieConsent" class="navbar navbar-default navbar-fixed-top" role="alert">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#cookieConsent .navbar-collapse">
                    <span class="sr-only">Toggle cookie consent banner</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <span class="navbar-brand"><span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span></span>
            </div>
            <div class="collapse navbar-collapse">
                <p class="navbar-text">
                    Use this space to summarize your privacy and cookie use policy.
                </p>
                <div class="navbar-right">
                    <a asp-page="/Privacy" class="btn btn-info navbar-btn">Learn More</a>
                    <button type="button" class="btn btn-default navbar-btn" data-cookie-string="@cookieString">Accept</button>
                </div>
            </div>
        </div>
    </nav>
    <script>
        (function () {
            document.querySelector("#cookieConsent button[data-cookie-string]").addEventListener("click", function (el) {
                document.cookie = el.target.dataset.cookieString;
                document.querySelector("#cookieConsent").classList.add("hidden");
            }, false);
        })();
    </script>
}

edit _Layout.cshtml to add the partial view to the following section:

 <div class="container body-content">
        <partial name="_CookieConsentPartial" />
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; 2018 - EVEStandard.ASPNETCoreSample</p>
        </footer>
    </div>

The cookies popup should appear, after agreeing the session storage will work and callback will be able to be completed.

ghost commented 3 years ago

See https://github.com/gehnster/EVEStandard-Examples/pull/3 for potential resolution for this issue.