gpt4 book ai didi

c# - 如何在 ASP.NET Core 5 Blazor 服务器模式下要求通过重定向登录进行身份验证

转载 作者:行者123 更新时间:2023-12-05 02:48:54 25 4
gpt4 key购买 nike

如何配置 BlazorHub 端点以要求经过身份验证的用户在未通过身份验证的情况下自动重定向登录?

我已尝试配置默认授权策略并在 BlazorHub 端点构建器上调用 RequireAuthenticated,但在运行 Blazor 应用程序时我仍未重定向到登录页面。

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(); // added by me
services.AddAuthorization(options =>
{
options.DefaultPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
}); // added by me
}

// 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();
}
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(); // added by me
app.UseAuthorization(); // added by me

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

最佳答案

我是这样做的

services.AddRazorPages()
.AddRazorPagesOptions(options
=> options.Conventions
.AuthorizeFolder("/")
);

关于c# - 如何在 ASP.NET Core 5 Blazor 服务器模式下要求通过重定向登录进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64261456/

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