How to add cookie consent in ASP.NET 6

📅 Published 04/07/2023

Let's configure our Program.cs class by add this configuration:

Add following code in your Program 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 🗙