Introduction
What exactly is "Consent"? Let's understand it.
Consent is nothing but taking permission or approval from the user that we are going to store their personal information based on some policy which we need to show them.
GDPR (which is General Data Protection Regulation) is one of the examples of a consent requirement which is a regulation of EU (European Union) law for data protection and privacy in the European Union.
So based on the above law, it is now compulsory to take consent from the user while accessing a website which is asking for personal information from the user.
So as a result, the website needs to ask the user to accept the consent when they first visit a website which is GDPR (or any other law) enabled.
So the good news is ASP.NET Core hasan in-built feature for asking and tracking consent for specific users, because now-a-days web sites store cookies on their browser which has some personal information.
How to use it?
Another piece of good news is that when you create a new ASP.NET Core project in VS 2019, a new partial view named _CookieConsentPartial.cshtml is automatically added in your project. So it means the template is ready.
This feature comes from the below namespace.
- @using Microsoft.AspNetCore.Http.Features
Next we need to set up something in Startup.Configure file in the project to enable consent.
-
- public void ConfigureServices(IServiceCollection services)
- {
- services.Configure<CookiePolicyOptions>(options =>
- {
- options.CheckConsentNeeded = context => true;
- options.MinimumSameSitePolicy = SameSiteMode.None;
- });
- }
-
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
- {
- app.UseCookiePolicy(););
- }
In the next stepwe just need to create one partial view named "_CookieConsentPartial.cshtml" under shared folder as below.
- @using Microsoft.AspNetCore.Http.Features
-
- @{
- var consentFeatureFlag = Context.Features.Get<ITrackingConsentFeature>();
- var showBannerFlag = !consentFeatureFlag?.CanTrack ?? false;
- var cookieStr = consentFeatureFlag?.CreateConsentCookie();
- }
-
- @if (showBannerFlag)
- {
- <div id="cookieConsentdiv" class="required classes">
- Please read our website privacy and policy <a asp-page="/Privacy">Learn More</a>.
- <button type="button" class="accept-policy close" data-cookie-string="@cookieStr">
- <span aria-hidden="true">Accept</span>
- </button>
- </div>
- <script type="text/javascript">
- $(document).ready(function () {
- $("#cookieConsentdiv button[data-cookie-string]").bind("click", function () {
- document.cookie = $("#cookieConsentdiv button").attr("data-cookie-string");
- $("#cookieConsentdiv").hide();
- });
- });
- </script>
- }
Once we've finished the above creation then add that partial view in _Layout.cshtml as below per your design requirement.
- <div class="container otherclassname">
- <partial name="_CookieConsentPartial" />
- <main role="main" class="myrenderclass">
- @RenderBody()
- </main>
- </div>
We are all set, our consent feature is ready to serve you.
So we have all the required guidelines finished in this feature for GDPR compliance.
In the latest ASP.NET Core version this template and configuration are automatically added by Visual Studio.
Hope this blog would help you!