gpt4 book ai didi

c# - AuthorizeRouteView Authorizing 和 NotAuthorized 参数设置

转载 作者:行者123 更新时间:2023-12-04 11:55:37 27 4
gpt4 key购买 nike

我想使用 NotAuthorized <AuthorizeRouteView> 中的属性每次非登录用户尝试访问页面时重定向到登录页面的标记。

然而,它需要一个 RenderFragment<AuthentificationState>类型参数。我应该设置什么来设置这个参数来呈现登录页面?

编辑:代码非常简单。我使用了身份存储在应用程序中的 Blazor 服务器端项目模板,只是添加了 RedirectToLogin.razor像这样 :

@inject NavigationManager NavigationManager
@code {
protected override void OnAfterRender()
{
NavigationManager.NavigateTo("counter"); //for an unknown reason, the "Identity/Account/Login" redirect doesn't work.
}
}

并修改了 App.razor :
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@if(true) { } //Used for breakpoint.
<RedirectToLogin />
</NotAuthorized>
<Authorizing>
@if(true) { } //Used for breakpoint.
</Authorizing>
</AuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>

我没有碰 Startup.cs所以它看起来像这样:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
services.AddSingleton<WeatherForecastService>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
}

最佳答案

我遇到了类似的问题,我的 <NotAuthorized>未为未经授权的用户显示 app.razor 中的部分。在拔出我的头发 3 天后,我也在其他答案中提到的 MainLayout.razor 中寻求解决方案。一个干净的项目的最后一次尝试让我意识到我是一个多么可怜的程序员,因为我终于找到了答案。
我没有完全阅读文档,我可以在其中找到问题的原因。在下一页:https://docs.microsoft.com/en-us/aspnet/core/blazor/security/?view=aspnetcore-5.0#customize-unauthorized-content-with-the-router-component
你会发现 NotAuthorized 部分是如何被调用的。我完全错过了第二个要点:

The Router component, in conjunction with the AuthorizeRouteViewcomponent, allows the app to specify custom content if:

  • Content isn't found.
  • The user fails an [Authorize] condition applied to the component. The [Authorize] attribute is covered in the [Authorize] attributesection.
  • Asynchronous authentication is in progress.

这意味着 <NotAuthorized>仅当路由端点具有 Authorize 标记时才会调用/显示部分。在我的情况下,路线将进入我的索引页面,没有授权标签......

关于c# - AuthorizeRouteView Authorizing 和 NotAuthorized 参数设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58995559/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com