在 Blazor 中使用 AAD B2C 登录后,如何控制用户定向到的页面?

How do I control the page to which a user is directed after logging in using AAD B2C in Blazor?

我有一个托管的 WASM 网站,使用 Azure 的 AAD B2C 进行用户身份验证和授权。网站的根是一个简单的登陆页面,向用户显示一个按钮,将用户带到授权页面,这与根据教程在 https://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/hosted-with-azure-active-directory-b2c?view=aspnetcore-5.0

创建的默认页面相同

我希望用户在完成登录过程后被重定向到特定页面 - 而不是返回到登录页面(而不是他们所在的带有 [authorize] 标签的页面,但他们没有授权访问)。我尝试将 returnUrl 设置为我希望用户在登录后被定向到的页面的 url ( Navigation.NavigateTo($"authentication/login?returnUrl=http://localhost:5000/ Start"); ), 但我们仍然被引导回到根页面。

您应该有一个身份验证组件,其中有一个 RemoteAuthenticatorView 组件,并且有一个 OnLogInSucceeded 参数,您可以使用该参数注册一个在用户登录时调用的方法 - 在此方法中,您可以设置state.ReturnUrl 重定向到任何你喜欢的地方,例如计数器如下图:

Authentication.razor

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

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

 void LoginSuccessHandler(RemoteAuthenticationState state)
 {
  state.ReturnUrl = "Counter";
 }
}