【问题标题】:Blazor Wasm setting end user defined startpageBlazor Wasm 设置最终用户定义的起始页
【发布时间】:2022-01-01 02:27:12
【问题描述】:

在我的应用程序中,我为不同类型的用户提供了许多区域。由角色控制。 所以我想给用户一个设置首选起始页的选项。

深度链接有效,所以没有问题。

我的第一次尝试是这样的

@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

【问题讨论】:

    标签: azure-active-directory blazor-webassembly


    【解决方案1】:

    请检查以下几点是否可以解决。

    1. 让页面(组件)准备好参与路由的第一步是为组件分配一个路由。您可以使用 page 指令在 cshmtl 文件中执行此操作。 @page@inject NavigationManager NavManager 上方的“/redirectpage”

    2. 确保在apps.razor和LoginDisplay组件中添加CascadingAuthenticationState和&lt;AuthorizeView&gt;。它负责显示用户有权查看的页面。 (或)

    3. 如果路由的页面组件包含授权属性(@attribute [Authorize]),则用户必须登录,否则将被重定向到登录页面。

    示例:如果您想保护 Counter.razor 页面的安全,只需在顶部添加一个 Authorize 属性:

       @using Microsoft.AspNetCore.Authorization
          @attribute [Authorize]

    或者

    @attribute [Authorize(Roles = "finance")]

    将此添加到需要身份验证的页面。

    参考资料:

    1. Secure an ASP.NET Core Blazor WebAssembly standalone app with the Authentication library | Microsoft Docs
    2. Using Azure Active Directory to Secure Blazor WebAssembly Hosted Apps (code-maze.com)
    3. Secure an ASP.NET Core Blazor WebAssembly standalone app with Azure Active Directory | Microsoft Docs

    【讨论】:

    • 谢谢,我知道了。它已经实现了
    猜你喜欢
    • 2021-04-12
    • 2020-10-17
    • 2022-10-22
    • 2021-07-17
    • 2021-05-14
    • 2023-04-04
    • 2020-07-27
    • 2021-10-01
    • 2021-11-16
    相关资源
    最近更新 更多