如何为每个客户存储一个唯一的julazor sessionID?
我们使用共享的Razor类库来用于包含所有组件的大型应用程序,即我们的Blazor Server和Blazor WebAssembly启动项目正在使用。
但是,当我们打开另一个选项卡或浏览器窗口时,转到同一URL时,我们注意到会话持续存在。
我们尝试在JavaScript中创建独特的GUID,并使用 ijsruntime 将其调用到该应用程序中。然后在所有组件内使用该独特的GUID来显示我们API加载的正确数据。但是,它并不总是有效的,也不是良好的精力。
因为我们正在使用共享的剃须刀库,所以我们无法使用 preectedsessionStorage 。我们如何在不使用JavaScript Interop调用的情况下获得唯一的会话?
We are using a shared Razor Class Library for our Blazor applications containing all components, that both our Blazor Server and Blazor WebAssembly startup projects are using.
However, when we open another tab or browser window, going to the same URL, we noticed that the session persists.
We tried creating an unique guid in JavaScript, and invoking it to the app using IJSRuntime. That unique guid is then used inside all components to show the correct data loaded from our API. It doesn't always work however, and it is not good perfomance wise either.
Because we are using a shared Razor Class Library, we are not able to use the ProtectedSessionStorage. How can we get unique sessions without using JavaScript interop calls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了给每个窗口/浏览式启用一个唯一的ID,只需在program.cs中添加一个示波器服务即可。如果这是您想要的。
应用程序的每个实例都会获得一个可以传递给您的库组件的唯一ID,以便在此会话持续的情况下识别选项卡/窗口实例。
的代码
Glazor webassaly添加程序
。CS添加myidService.cs类
添加/注入您需要此行为的每个页面。或使其更简单,只需将其添加到共享/mainlayout.razor文件中即可。
只要用户会话持续,ID才是好的。如果要存储每个用户的状态,供以后使用。您将需要使用UserLogin和某种类型的数据列表。
To give each Window/BrowserTab-Session a unique-Id just add a scoped service in Program.cs. If this is what your want.
Each instance of the App gets a unique Id that can be passed on to your library-components, in order to identify a tab/window instance as long as this session lastes.
Code for Blazor Webassably
Add in Program.cs
Add the MyIdService.cs class
Add/inject this to every page you need this behavior. Or to make it more simple just add it to the shared/mainlayout.razor file.
The Id is only good as long as a user session lasts. If you want to store state of every user for later use. You would need a userlogin and some sort of datapersisting.