且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Blazor Server中设置同意cookie

更新时间:2023-12-03 15:47:58

昨天我刚刚在Blazor 3.1应用中解决了这个问题!我选择使用JS Interop,这非常容易.

I just solved this in a Blazor 3.1 app yesterday! I opted to use JS Interop and it was super easy.

我的Web应用程序具有多个前端,包括MVC,Razor Pages和Blazor,因此您可以打开主解决方案文件,然后检出Blazor项目:

My web app has multiple front ends, including MVC, Razor Pages and Blazor, so you can open the main solution file and then check out the Blazor project:

https://github.com/shahedc/NetLearnerApp

Blazor项目中的注意事项;

Things to note in the Blazor project;

  • 我将局部视图实现为剃刀组件
  • 在共享"文件夹中查找/_CookieConsentPartial.razor
  • MainLayout.razor使用此剃须刀组件

  • I implemented the partial view as a razor component
  • look for /_CookieConsentPartial.razor in the Shared folder
  • MainLayout.razor uses this razor component

_Host.cshtml文件包括我的js互操作文件

The _Host.cshtml file includes my js interop file

js Interop文件包含document.cookie用法

the js Interop file contains the document.cookie usage

剃刀组件使用JSRuntime.InvokeVoidAsync调用JS方法以接受GDPR同意消息并存储cookie

the razor component uses JSRuntime.InvokeVoidAsync to call the JS method to accept the GDPR consent message and store the cookie

希望有帮助!