How to add cookie consent in ASP.NET Core 5.0

📅 Published 08/11/2021

If you're looking for the ASP.NET 6 version of this article, you can find it here.

Let's configure our Startup.cs class by add this configuration and using statement.

Add the using statement:

Add following code in the ConfigureServices method of your Startup class:

Add following code in the Configure method of your Startup class:

Example:

Add a new Razor page in the Shared folder with the name: `_CookieConsentPartial.cshtml' and add following code in this new page:

Now add the partial tag-helper in your `_Layout.cshtml' page:

You can place this partial tag-helper anywhere you want in your HTML code.

If you run your web application you should see something like this:

cookie consent example

If you click Accept then you'll see the cookie message disappear.
Refresh your page and you'll see that the message doesn't come back.

An unhandled error has occurred. Reload 🗙