Blazor Wasm 身份登录/注销事件

时间:2021-03-10 16:15:55

标签: authentication blazor identityserver4 webassembly

在使用来自 Wasm 模板的标准身份的 .Net 5 Blazor WASM 应用程序中,我想在用户登录(或注销)时执行某些操作。我在 Authentication.razor 中尝试了 RemoteAuthenticatorView:

@page "/authentication/{action}"
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
<RemoteAuthenticatorView Action="@Action" 
                         OnLogInSucceeded="OnLoginSucceeded"
                         OnLogOutSucceeded="OnLogoutSucceeded1"
                         />


@code{
    [Parameter] public string Action { get; set; }



    public async void OnLoginSucceeded()
    {
        // do something
    }

    public void OnLogoutSucceeded1()
    {
        // do something
    }

}

而且,我尝试了一个组件(我真的想在其中执行合并购物篮的操作):

AuthenticationStateProvider.AuthenticationStateChanged += OnAuthenticationStateChanged;


 private async void OnAuthenticationStateChanged(Task<AuthenticationState> task)
    {
        var user = (await task).User;
        Console.WriteLine(DateTime.Now.ToString("hh:mm:ss:fff") + " InitialDataLoader -> OnAuthenticationStateChanged -> IsUserAuthenticated: " + user.Identity.IsAuthenticated);

    }

我在每个方法中都有断点,在我最终在 RemoteAuthenticatorView 和我的组件中命中登录断点后(它有一段时间没有中断),它现在中断但只有一次。如果我注销 - 没有注销事件。如果我再次登录,即使是其他用户,也不会遇到断点。

是否有一致的方法来生成或检测登录信息?

1 个答案:

答案 0 :(得分:0)

事实证明它工作正常,只是断点不起作用 - 不知道为什么。我向每个和所有事件都添加了 Console.WriteLine 消息正确命中。所以这两种技术都按预期工作。