我使用.NET Core 3.0创建了一个全新的Blazor服务器端项目,并关闭了未经身份验证用户的应用程序。
我现在正尝试通过在文件顶部放置[AllowAnonymous]来允许匿名访问Index.razor组件。但是,这似乎无能为力。
场景
使用默认的Blazor模板“ WeatherForecast”后,我将以下内容添加到 Startup.cs
services.AddMvcCore(options =>
{
var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
如果用户未通过身份验证,则这段代码将阻止对我的应用程序的所有请求。
添加了这段代码后,我想为未经身份验证的用户打开默认的 Index.razor 组件。为此,我将 @attribute [AllowAnonymous] 添加到Index.razor:
@page "/"
@attribute [AllowAnonymous]
<h1>Hello,world!</h1>
Welcome to your new app.
App.razor
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(Mainlayout)" />
</Found>
<NotFound>
<CascadingAuthenticationState>
<LayoutView Layout="@typeof(Mainlayout)">
<p>Sorry,there's nothing at this address.</p>
</LayoutView>
</CascadingAuthenticationState>
</NotFound>
</Router>
预期结果
运行我的应用程序时,未经身份验证的用户将被允许访问https://localhost:XXXX处的索引页面
实际结果
我的用户将转发到我的OpenIdConnect URI。