【发布时间】:2020-03-18 14:29:19
【问题描述】:
我想使用<AuthorizeRouteView>标签中的NotAuthorized属性在每次非登录用户尝试访问页面时重定向到登录页面。
但是,它需要一个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");
});
}
}
【问题讨论】:
-
在 OnInitialised() 中尝试使用 NavigationManager 时总是报错,所以只能在 OnAfterRender 中使用。顺便说一句,我正在使用.Net Core 3.0,即使这样,它也永远不会进入任何可能的 2 个授权标签,我检查了我的
if(true) { }语句。否则,即使没有被识别,它也会直接认为用户已获得授权,或者它甚至不关心这些标签。 -
您尝试升级到 3.1-preview3 吗?
-
您的主布局中有
<AuthorizedView>或AuthorizeAttribute吗? -
你需要这个:NavigationManager.NavigateTo("Identity/Account/Login", forceLoad:true);看我的回答。
-
@aguafrommars 不,但这有什么改变吗?不应该通过检查用户是否被授权在路由之前触发重定向吗?而且我现在不会升级到预览版,除非非常必要,因为该应用程序应该在市场上。 (抱歉回复晚了,周末没在电脑上)
标签: c# asp.net blazor blazor-server-side