[英]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:对于您的场景:
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>
Create a new Razor page and put the "Authorize" tag on it and use the _LayoutBlazor.创建一个新的 Razor 页面并在其上放置“授权”标签并使用 _LayoutBlazor。
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.