在我的应用程序中,我有许多适合不同类型用户的区域。由角色控制。所以我想给用户一个选项来设置一个首选的起始页。
深度链接有效,所以没有问题。
我的第一次尝试是这个
@code{
[Parameter] public string Action { get; set; }
[Inject] private NavigationManager Navigation { get; set; }
private void LoginSucceeded(RemoteAuthenticationState state)
{
Console.WriteLine("navigate to DepartmentAccess");
// This works with on extra login
Navigation.NavigateTo("/DepartmentAccess", true);
// This loads 3 or more times
// state.ReturnUrl = "/DepartmentAccess";
}
}
然后我尝试在RedirectToLogin.razor中更改返回url。在这里我添加了"MyStartPage">
@inject NavigationManager Navigation
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Zeus.Client.PortfolioRights
@code {
protected override void OnInitialized()
{
Navigation.NavigateTo($"authentication/login?returnUrl={Uri.EscapeDataString(Navigation.Uri)/MyStartPage}");
}
}
这绝对没有效果!
好了,是时候再深入一点了
RemoteAuthenticatorViewCore保存与登录过程相关的所有代码。此函数用于处理登录状态。它是在从Azure AD 重定向后调用的
private async Task ProcessLogIn(string returnUrl)
{
AuthenticationState.ReturnUrl = returnUrl;
var result = await AuthenticationService.SignInAsync(new RemoteAuthenticationContext<TAuthenticationState>
{
State = AuthenticationState
});
switch (result.Status)
{
case RemoteAuthenticationStatus.Redirect:
break;
case RemoteAuthenticationStatus.Success:
await OnLogInSucceeded.InvokeAsync(result.State);
await NavigateToReturnUrl(GetReturnUrl(result.State, returnUrl));
break;
case RemoteAuthenticationStatus.Failure:
_message = result.ErrorMessage;
Navigation.NavigateTo(ApplicationPaths.LogInFailedPath);
break;
case RemoteAuthenticationStatus.OperationCompleted:
default:
throw new InvalidOperationException($"Invalid authentication result status '{result.Status}'.");
}
}
输入参数"returnUri"由函数设置
private string GetReturnUrl(TAuthenticationState state, string defaultReturnUrl = null)
{
if (state?.ReturnUrl != null)
{
return state.ReturnUrl;
}
var fromQuery = QueryStringHelper.GetParameter(new Uri(Navigation.Uri).Query, "returnUrl");
if (!string.IsNullOrWhiteSpace(fromQuery) && !fromQuery.StartsWith(Navigation.BaseUri))
{
// This is an extra check to prevent open redirects.
throw new InvalidOperationException("Invalid return url. The return url needs to have the same origin as the current page.");
}
return fromQuery ?? defaultReturnUrl ?? Navigation.BaseUri;
}
所以我想知道。为什么在更改返回uri时未设置"defaultReturnUrl"?
Navigation.NavigateTo($"authentication/login?returnUrl={Uri.EscapeDataString(Navigation.Uri)/MyStartPage}
我想我不了解登录流程。但有一种感觉,我很接近。只需要找到一种方法来设置defaultReturnUrl
请检查以下几点是否可以解决。
-
让页面(组件(准备好参与路由的第一步是为组件分配路由。您可以在cshmtl文件中使用page指令执行此操作。CCD_ 1"/重定向";以上@inject NavigationManager NavManager
-
请确保在apps.razor和LogiDisplay组件中添加CascadingAuthenticationState和
<AuthorizeView>
。它负责显示用户有权查看的页面。(或( -
如果路由的页面组件包含authorize属性(
@attribute [Authorize]
(,则用户必须登录,否则将重定向到登录页面。
示例:如果您想保护Counter.razor页面的安全,只需在顶部添加Authorize属性:
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]
或
@attribute〔Authorize(Roles="finance"(〕
将其添加到需要身份验证的页面中。
参考文献:
- 使用身份验证库|Microsoft文档
- 使用Azure Active Directory保护托管的Blazor WebAssembly应用程序(code maze.com(
- 使用Azure保护ASP.NET Core Blazor WebAssembly独立应用程序Active Directory | Microsoft文档