Blazor托管-在显示应用程序之前立即进行授权 [英] Blazor hosted - authorize immediately before displaying application

查看:116
本文介绍了Blazor托管-在显示应用程序之前立即进行授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最初显示该应用程序之前,任何人都可以帮助我让托管的Blazor应用程序(客户端,服务器和共享)立即请求登录.我希望用户完全访问该应用程序之前必须先登录.

Can anybody give me a helping push to get my hosted Blazor application (Client, Server and Shared) to request a login immediately, before the application is initially shown. I want the experience that a user must log in before accessing the application at all.

我的起点是具有Api授权(单个用户帐户)的Blazor Webassembly(托管)模板

My starting point is the Blazor Webassembly (hosted) template with Api Authorization (Individual User Accounts)

在服务器端操作或客户端Razor页面上使用Authorize属性不会在用户请求具有Authorize属性的特定操作/页面之前启动身份验证流程.在首次显示该应用程序之前,我该如何首先启动授权流程?

Using the Authorize attribute on either server-side actions or a client-side Razor page will not initiate the authentication flow before the specific action/page with the Authorize attribute is being requested by the user. How would I go about having the authorization flow kicked off as the first thing, before the application is even displayed for the first time?

我敢肯定,对于比我更精明的人来说,这是可能的,甚至是微不足道的.有人可以朝正确的方向推我吗?

I am sure this is possible and even trivial for somebody more savvy than me. Can anybody give me a shove in the right direction, please?

推荐答案

我创建了一个控件RedirectToLogin.razor

I created a control RedirectToLogin.razor

@inject NavigationManager Navigation
@code {
    protected override void OnInitialized()
    {
        String thisPage = Navigation.Uri.Replace(Navigation.BaseUri, "~/");
        Navigation.NavigateTo($"Identity/Account/Login?returnUrl={thisPage}");
        base.OnInitialized();
    }
}

然后将其插入mainlayout.razor

And then inserted it into the mainlayout.razor

<div class="container-fluid">
 <AuthorizeView>
        <Authorized>
            <NavigationLogger />
            <ContextMenuMouseClick>
                <MenuTopBar />
                <NavMenu />
                <SubPageContainer>
                    @Body
                </SubPageContainer>
            </ContextMenuMouseClick>
        </Authorized>
        <NotAuthorized>
            <RedirectToLogin />
        </NotAuthorized>
</AuthorizeView>
</div>

因此,当加载布局并且它处于NotAuthorized状态时,它将重定向到登录页面,并且在授权后将返回到它试图访问的页面.

So when the layout is loaded and it is in the NotAuthorized state it will redirect to the login page and after authorising will return to the page it was trying to access.

希望这会有所帮助.

这篇关于Blazor托管-在显示应用程序之前立即进行授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆