简体   繁体   English

Blazor 托管 - 在显示应用程序之前立即授权

[英]Blazor hosted - authorize immediately before displaying application

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.任何人都可以帮我推动我托管的 Blazor 应用程序(客户端、服务器和共享)在最初显示应用程序之前立即请求登录。 I want the experience that a user must log in before accessing the application at all.我想要用户在访问应用程序之前必须登录的体验。

My starting point is the Blazor Webassembly (hosted) template with Api Authorization (Individual User Accounts)我的出发点是带有 Api 授权(个人用户帐户)的 Blazor Webassembly(托管)模板

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.在用户请求具有Authorize属性的特定操作/页面之前,在服务器端操作或客户端 Razor 页面上使用Authorize属性不会启动身份验证流程。 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?谁能给我一个正确的方向,好吗?

I created a control RedirectToLogin.razor我创建了一个控件 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();
    }
}

And then inserted it into the mainlayout.razor然后插入到mainlayout.razor

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

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.因此,当加载布局并且它位于 NotAuthorized state 中时,它将重定向到登录页面,授权后将返回到它试图访问的页面。

Hope this helps.希望这可以帮助。

The Blazor Client is bootstrapped from a static index.html in the wwwroot . Blazor 客户端从wwwroot中的 static index.html引导。 In the Server project this is mapped to an Endpoint in the Startup.cs , basically a catch-all to all endpoints not taken by your Razor Pages or Controllers:在 Server 项目中,它映射到Startup.cs中的 Endpoint,基本上是所有未由 Razor 页面或控制器采用的端点:

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
    endpoints.MapControllers();
    endpoints.MapFallbackToFile("index.html");
});

For your scenario:对于您的场景:

  1. Create a new layout in Server, say _LayoutBlazor.cshtml在服务器中创建一个新布局,比如 _LayoutBlazor.cshtml
  2. Re-create the contents of the index.html from the Client.从客户端重新创建index.html的内容。 Note that the _framework files from the Client are copied to the Server wwwroot upon build: :请注意,来自客户端的 _framework 文件在构建时被复制到服务器 wwwroot: :

    <app>Loading...</app>

    <script src="@Href("~/_framework/blazor.webassembly.js")"></script>

  3. Create a new Razor page and put the "Authorize" tag on it and use the _LayoutBlazor.创建一个新的 Razor 页面并在其上放置“授权”标签并使用 _LayoutBlazor。

  4. Remove the endpoints.MapFallbackToFile("index.html");删除endpoints.MapFallbackToFile("index.html"); from Startup.cs来自 Startup.cs

Mark Gould has created a proof of concept here: RazorBlazor Mark Gould 在这里创建了一个概念证明: RazorBlazor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Blazor WASM 托管 - 在 API 上授权始终返回未经授权 - Blazor WASM Hosted - Authorize on API Always returns UnAuthorized Blazor WebAssembly授权 - Blazor WebAssembly Authorize Blazor wasm 托管应用程序与公司 AD windows 身份验证 - Blazor wasm hosted application with company AD windows authentication 是否可以直接路由到托管在 MVC 应用程序中的 blazor 页面? - Is it possible to route directly to a blazor page hosted in an MVC application? Blazor @attribute [Authorize] 标签不起作用 - Blazor @attribute [Authorize] tag is not working Null Blazor 托管的 ConfigureServices 中的 WebRootPath - Null WebRootPath in ConfigureServices for Blazor Hosted 为托管的 blazor 实现进度条 - Implementing progress bar for a blazor hosted Blazor 服务器无法访问具有 [Authorize] 属性的控制器 - Blazor Server cant access controller with [Authorize] attribute 在提供托管的 Blazor WebAssembly 应用程序之前,如何使用 Azure AD B2C 对服务器上的用户进行身份验证? - How can I use Azure AD B2C to authenticate users on the server before serving a hosted Blazor WebAssembly app? 在 C# Blazor Web 程序集(ASP.NET Core 托管)应用程序中放置服务文件夹的最佳实践? - Best practice for where to place Services folder in C# Blazor Web Assembly (ASP.NET Core hosted) application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM