Blazor服务器-通过保持根文件夹的授权使特定页面匿名



我使用的是.NET5 Blazor服务器应用程序。我在Startup.cs中有以下代码,它确保在用户未通过身份验证的情况下始终显示OKTA登录页面。

services.AddRazorPages(options =>
{
options.Conventions.AuthorizeFolder("/");
options.Conventions.AllowAnonymousToPage("/anonymous");
//No error here but no effect either
}
);

如果你注意到我想访问一个页面"/匿名";,wthout身份验证。但它总是在通往俄克拉荷马州的路上。我怎样才能跳过这个路由并直接访问"/匿名者";?我必须保留线路选项。Convention.AuthorizeFolder("/"(;

我的App.razor已经启用了CascadingAuthenticationState视图,该视图也需要保留。

<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<h4>Sorry, you're not authorized to reach this page.</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>

我也试过appied@attribute[AllowAnonymous]。任何线索都将不胜感激。

当您需要匿名页面时,我收到了一个解决方法,用.CSHTML文件代替.rarzor文件。诸如";选项。Conventions.AllowAnonymousToPage";没有考虑.rarzor页面,但可以很好地使用.CSHTML文件。我保留这条线索,以便获得任何解决方案来申请.rarzor文件。然后将决定使用.CSHTML文件进行匿名访问。

最新更新