Blazor Webassembly 已验证事件事件、Blazor、Webassembly

2023-09-06 18:07:32 作者:战场英雄袍

在使用 Microsoft.AspNetCore.Components.WebAssembly.Authentication 对用户进行身份验证时,在托管在 ASP.NET Core(最新版,blazor)上的客户端应用程序上的 Blazor Webassembly 应用程序中,每次何时/如何调用操作用户已通过身份验证?即使他们回到应用并进行了活跃的会话.

When authenticating users with Microsoft.AspNetCore.Components.WebAssembly.Authentication, in a Blazor Webassembly application on the client side app that is hosted on ASP.NET Core (latest, blazor), when/how to call an action every time a user is authenticated? Even if they come back to the app and have an active session.

2020 年 7 月 15 日更新:我要克服的问题是,一旦用户登录 (MSAL) 以获取他们的信息和个人资料图像,我们就有一项服务会转到 Graph API.他们登录后,我使用 RemoteAuthenticatorView.OnLogInSucceeded 事件来了解他们实际登录的时间.问题是,如果他们随后刷新页面 (F5),则服务会丢失 Graph 信息.我想我实际上想要实现的是持久化会话数据.

Update 2020-07-15: The problem I am trying to overcome is that we have a service that goes to Graph API once a user logs in (MSAL) to get their info and profile image. After they log in, I use the RemoteAuthenticatorView.OnLogInSucceeded event to know when they actually logged in. Trouble is, if they then refresh the page (F5), the service loses the Graph info. I guess what I'm actually trying to achieve is to persist session data.

推荐答案

为了克服这个问题,到目前为止,我已经解决了在 LoginDisplay 组件的 razor 中使用 Authorized 部分来触发服务以检查服务用户的解决方法info 为空,然后再次转到 Graph,而不是尝试存储在 localstorage 或类似的东西中......然后,我让用户信息显示组件订阅服务中的事件,以了解何时需要更新用户信息以调用 StateHasChanged().

To overcome this, I so far settled with the workaround to use the Authorized section in razor of the LoginDisplay component, to trigger the service to check if the service user info is empty and then go to Graph again instead of trying to store in in localstorage or something like that... Then, I have user info displaying components subscribe to an event in the service to know when the user info needed updated to call StateHasChanged().

如果有更好的解决方案来跨页面刷新持久化服务数据,我会全力以赴.

If there is a better solution to persisting service data across page refreshes, I am all eyes.