Asp.net core 重定向到Blazor服务器Azure B2C身份验证中的登录
尝试将未经身份验证的用户重定向到登录页,而不是显示空白索引页 我尝试将app.razor修改为重定向,如下所示:Asp.net core 重定向到Blazor服务器Azure B2C身份验证中的登录,asp.net-core,azure-ad-b2c,blazor-server-side,azure-authentication,Asp.net Core,Azure Ad B2c,Blazor Server Side,Azure Authentication,尝试将未经身份验证的用户重定向到登录页,而不是显示空白索引页 我尝试将app.razor修改为重定向,如下所示: <NotAuthorized> @if (!context.User.Identity.IsAuthenticated) { <RedirectToLogin /> } else
<NotAuthorized>
@if (!context.User.Identity.IsAuthenticated)
{
<RedirectToLogin />
}
else
{
<p>
You are not authorized to access
this resource.
</p>
}
</NotAuthorized>
[CascadingParameter] protected Task<AuthenticationState> AuthStat { get; set; }
protected async override Task OnInitializedAsync()
{
base.OnInitialized();
var user = (await AuthStat).User;
if (!user.Identity.IsAuthenticated)
{
navMan.NavigateTo($"authentication/login?returnUrl={Uri.EscapeDataString(navMan.Uri)}");
}
}
如果没有在Blazor服务器端项目中使用Azure B2C身份验证进行身份验证,请有人建议如何将用户重定向到登录页面?您可以使用以下更改强制用户登录 方法1: _Cshtml添加以下代码
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]
App.razor
剃须刀
@身体
我很困惑。你发布了两个MainLayout.razor实际上我在建议两种不同的方法。我已经更新了我的答案,请检查。。我只是想自己解决这个问题。我看到您将[Authorize]
添加到\u Host.cshtml
-但对于B2C应用程序,这意味着Blazor路由从未被调用,它总是重定向到B2C登录屏幕,这是一种非常糟糕的用户体验。如果我添加了[AllowAnonymous]
属性,那么我可以允许访问Blazor页面,但在要求用户登录之前,使用您对MainLayout
的更改来显示欢迎内容。
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<h4>Not authorized.</h4>
</NotAuthorized>
<Authorizing>
<h4>Authentication in progress...</h4>
</Authorizing>
</AuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
@inherits LayoutComponentBase
<div class="sidebar">
<NavMenu />
</div>
<div class="main">
<AuthorizeView>
<NotAuthorized></NotAuthorized>
<Authorized>
<div class="top-row px-4 auth">
<LoginDisplay />
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
</div>
<div class="content px-4">
@Body
</div>
</Authorized>
</AuthorizeView>
</div>
<environment include="Staging,Production">
<component render-mode="ServerPrerendered" type="typeof(App)" />
</environment>
<environment include="Development">
<component render-mode="Server" type="typeof(App)" />
</environment>
@inject NavigationManager Navigation
@code {
protected override Task OnAfterRenderAsync(bool firstRender)
{
Navigation.NavigateTo("/AzureADB2C/Account/SignIn");
return base.OnAfterRenderAsync(firstRender);
}
}
<div class="main">
<div class="top-row px-4 auth">
<LoginDisplay />
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
</div>
<div class="content px-4">
<AuthorizeView>
<Authorized>
@Body
</Authorized>
<NotAuthorized>
<Blazor_B2C.Pages.RedirectToLogin></Blazor_B2C.Pages.RedirectToLogin>
</NotAuthorized>
</AuthorizeView>
</div>
</div>