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.
- //Below are the minimum configuration required to make it enable
- 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.

ServerXP ServerXPPosted Jun 4, 2021, 12:30 PM
How can I create a cookie consert in asp.net MVC ?
Pankajkumar PatelPosted Aug 4, 2020, 12:19 AM
Nice article with example.