在 host.cshtml 上设置 @attribute [Authorized] 时,Blazor 服务器允许匿名页面

问题描述 投票:0回答:2

我通过在hosts.cshtml中只允许授权用户并在app.razor中使用AuthorizeView标签来锁定我的应用程序。最终效果是强制所有未经授权的用户进入登录页面。

这种方法非常有效,直到薪水比我高的人决定其中一两页应该匿名。将页面设置为 @attribute [AllowAnonymous] 无效,因为永远无法访问该页面。

是否有策略来实现这一点,希望在 app.razor 中?

这是我的

hosts.cshtml
...

@page "/"

@using Microsoft.AspNetCore.Authorization

@attribute [Authorize]

@namespace U3A.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = "_Layout";
}

<component type="typeof(App)" render-mode="ServerPrerendered" />

并且

app.razor
看起来像这样:

<CascadingAuthenticationState>
    <Router AppAssembly="@typeof(App).Assembly">
        <Found Context="routeData">
            <AuthorizeView>
                <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
                <FocusOnNavigate RouteData="@routeData" Selector="h1" />
            </AuthorizeView>
        </Found>
        <NotFound>
            <PageTitle>Not found</PageTitle>
            <LayoutView Layout="@typeof(MainLayout)">
                <p role="alert">Sorry, there's nothing at this address.</p>
            </LayoutView>
        </NotFound>
    </Router>
</CascadingAuthenticationState>

谢谢你

authorization blazor-server-side
2个回答
0
投票

实现此目的的方法是在 Razor 组件本身中指定属性。

在所有需要授权的页面上添加此属性即可。只需省略应该对每个人都可用的页面即可。

@attribute [Authorize]

0
投票

谢谢您的回复。 ASP.NET 3.1 解决方案就在这里...

Blazor 允许匿名访问 Razor 页面

对于我的 Blazor 6 应用程序,我执行了以下操作...

  1. Pages 文件夹中创建一个名为 Public 的文件夹,并在其中...
  2. 创建文件 _PublicHost.cshtml
  3. 创建文件 _PublicLayout.cshtml
  4. 创建文件 PublicApp.razor
  5. 创建文件 PublicMainLayout.razor
  6. 创建文件 _Imports.rezor
  7. 创建了我的公共页面ClassScheduleView.razor
  8. 更改了 Program.cs 中的 MapFallbackToPage

_PublicHost.cshtml

@page "/Public"

@using Microsoft.AspNetCore.Authorization

@attribute [AllowAnonymous]

@namespace U3A.Pages.Public
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = "_PublicLayout";
}

<component type="typeof(PublicApp)" render-mode="ServerPrerendered" />

_PublicLayout.cshtml

此处不需要对标准 _Layout.cshtml 进行特殊更改。我读到

<base href="~/" />
应更改为
<base href="~/public" />
但这会导致第 3 方 DevExpress 组件中出现空引用异常。我保持原样,没有任何问题。

_进口.razor

这里没什么特别的。

PublicApp.razor

<Router AppAssembly="@typeof(App).Assembly">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(PublicMainLayout)" />
    </Found>
    <NotFound>
        <LayoutView Layout="@typeof(PublicMainLayout)">
            <p class="alert alert-warning">Sorry, there's nothing at this address.</p>
        </LayoutView>
    </NotFound>
</Router>

PublicMainLayout.razor

@inherits LayoutComponentBase

<div class="page">
    <div class="container-fluid">
        @Body
    </div>
</div>

@code {

}

ClassScheduleView.razor

这只是一个标准的 Blazor 页面。这里的技巧是确保正确的@page指令...

@page "/Public/ClassScheduleView"

程序.cs

app.MapBlazorHub();
app.MapFallbackToPage("/Public/{*path:nonfile}", "/Public/_PublicHost");
app.MapFallbackToPage("/_Host");

app.Run();
© www.soinside.com 2019 - 2024. All rights reserved.