[英]aspnetcore blazor navigation from razor component to razor page
I am attempting to redirect from a razor component to a razor page.我正在尝试从 razor 组件重定向到 razor 页面。 If user is not authorized I want to redirect from current razor component to Login Razor Page.
如果用户未获得授权,我想从当前 razor 组件重定向到登录 Razor 页面。
I have redirect to login component我已重定向到登录组件
public class RedirectToLogin : ComponentBase
{
[Inject]
protected NavigationManager NavigationManager { get; set; }
protected override void OnInitialized()
{
NavigationManager.NavigateTo("./Identity/Account/Login",true);
}
}
this line throws an error NavigationManager.NavigateTo("./Identity/Account/Login");此行抛出错误NavigationManager.NavigateTo("./Identity/Account/Login");
Microsoft.AspNetCore.Components.NavigationException: 'Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown.' Microsoft.AspNetCore.Components.NavigationException:“引发了‘Microsoft.AspNetCore.Components.NavigationException’类型的异常。”
The assumption i have come to is that the problem is routing from a razor component to a razor page.我的假设是问题是从 razor 组件路由到 razor 页面。
If someone is looking for a complete solution, this is how I did it.如果有人正在寻找完整的解决方案,我就是这样做的。
Step 1: Create a razor component named RedirectToLogin.razor
wherever you want.第 1 步:在任意位置创建名为
RedirectToLogin.razor
的 razor 组件。
For eg: I'm creating it inside Areas/Identity/Components例如:我在Areas/Identity/Components中创建它
And add the following code to this file:并将以下代码添加到此文件中:
@inject NavigationManager Navigation
@code {
[Parameter]
public string ReturnUrl { get; set; }
protected override async Task OnInitializedAsync()
{
ReturnUrl = "~/" + ReturnUrl;
Navigation.NavigateTo("Identity/Account/Login?returnUrl=" + ReturnUrl, true);
await base.OnInitializedAsync();
}
}
Step 2: Use this component from App.razor
inside <NotAuthorized></NotAuthorized>
:第 2 步:在
<NotAuthorized></NotAuthorized>
中使用App.razor
中的这个组件:
@inject NavigationManager Navigation
@using HMT.Web.Server.Areas.Identity.Components
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@if (!context.User.Identity.IsAuthenticated)
{
<RedirectToLogin ReturnUrl="@Navigation.ToBaseRelativePath(Navigation.Uri)" />
}
else
{
<p role="alert">Sorry, you're not authorized to view this page.</p>
}
</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
https://github.com/affableashish/blazor-server-auth/tree/feature/LayoutWithIdentityPages https://github.com/affableashish/blazor-server-auth/tree/feature/LayoutWithIdentityPages
Start the app and try to get to an authorized view from the browser (Counter page here as an example):启动应用程序并尝试从浏览器获取授权视图(此处以计数器页面为例):
You'll be redirected to Login page:您将被重定向到登录页面:
You'll log in successfully and get to the Counter page:您将成功登录并进入柜台页面:
I got a redirect to a not authorized page, using the Router below.我使用下面的路由器重定向到未经授权的页面。
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<div class="container">
<h1>Sorry</h1>
<p>You're not authorized to reach this page.</p>
<p>You may need to log in as a different user.</p>
<a href="Identity/Account/Login">Log in</a>
</div>
@*<RedirectToLogin />*@
</NotAuthorized>
<Authorizing>
<div class="container">
<h1>Authentication in progress</h1>
<p>Leave your hands on the keyboard, scanning your fingerprints. Ain't technology grand.</p>
</div>
</Authorizing>
</AuthorizeRouteView>
</Found>
<NotFound>
<CascadingAuthenticationState>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</CascadingAuthenticationState>
</NotFound>
</Router>
This sufficed for the app for now.这对于现在的应用程序来说已经足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.