Blazor - 在不同布局之间使用级联身份验证状态

oss*_*wmi 1 authentication layout razor blazor

我有一个 .NET Core 3.1 Blazor 项目,其中应用程序内有 3 个子文件夹,每个子文件夹提供一个页面,每个页面的内容将使用 Three.JS 由 WebGL 驱动。

在我正在测试的第一个文件夹中,我创建了两个项目。第一个名为“_TestPage.cshtml”,另一个名为“Index.razor”。两个页面都包含以下 @page 指令:

@page "/Applications/TestFolder1/Index"
Run Code Online (Sandbox Code Playgroud)

_TestPage.cshtml 页面包含以下内容:

<!DOCTYPE html>
<html lang="en">
    <body>
        <app>
            <component type="typeof(App)" render-mode="Server" />
        </app>
    </body>
    <head>
        <meta charset="utf-8" />

        <meta name="viewport" content="width=device-width, initial-scale=1.0" />

        <title>My Blazor App</title>

        <base href="/" />

        <script src="_framework/blazor.server.js"></script>
        <style type="text/css">
            body {
                background-color: red;
            }
        </style>
    </head>
</html>
Run Code Online (Sandbox Code Playgroud)

Index.razor 页面中还包含以下内容:

@layout EmptyLayout
@page "/Applications/Monitor/Index"

<AuthorizeView>
    <Authorized>
        <p style="color: black;">***I can not see this text either...***</p>
    </Authorized>
    <NotAuthorized>
        <RedirectNotAuthorizedAccess />
    </NotAuthorized>
</AuthorizeView>
Run Code Online (Sandbox Code Playgroud)

但是,当应用程序启动时,我可以直接进入子文件夹中的索引页面,而无需登录。我得到纯红色背景,但看不到任何呈现的文本,也没有重定向到登录页面应该是这样,因为我还没有登录。

当我不使用 Visual Studio 创建的默认“MainLayout”组件并使用 @layout 指令覆盖 razor 页面内的布局时,是否需要做一些不同的事情才能使级联身份验证状态正常工作?

Zan*_*lal 6

我以示例方式解决了这个问题并为我完美工作,

使用Blazor 身份验证和授权来检测用户是否通过 JWT 或授权用户的任何流程进行授权

用于更改用户身份验证或不使用GetAuthenticationStateAsync覆盖方法

我正在使用 JWT 来检测用户登录是否成功

string token = await TokenAsync();
        ClaimsIdentity identity = await IsAuthenticated(token)
            ? new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "mrfibuli"), "Fake authentication type") // true, auth
            : new ClaimsIdentity(); // false, no auth
        return new AuthenticationState(new ClaimsPrincipal(identity));
Run Code Online (Sandbox Code Playgroud)

您可以使用链接始终为真吗

那么您可以更改App.razor来响应AuthenticationStateProvider 服务吗

应用剃刀

    <CascadingAuthenticationState>
    <Router AppAssembly="@typeof(Program).Assembly">
        <Found Context="routeData">
            <AuthorizeView>
                <Authorized>
                    <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
                </Authorized>
                <NotAuthorized>
                    <CascadingAuthenticationState>
                        <RedirectToLogin />
                    </CascadingAuthenticationState>
                </NotAuthorized>
            </AuthorizeView>
        </Found>
        <NotFound>
            <CascadingAuthenticationState>
                <LayoutView Layout="@typeof(EmptyLayout)">
                    <p>No Found</p>
                </LayoutView>
            </CascadingAuthenticationState>
        </NotFound>
    </Router>
</CascadingAuthenticationState>
Run Code Online (Sandbox Code Playgroud)

<RedirectToLogin />如果用户过期或没有身份验证,则仅创建重定向登录

RedirectToLogin.razor

    @page "/NotAuthorized/RedirectToLogin"
    <iServicePayroll.Pages.Home.Login />
Run Code Online (Sandbox Code Playgroud)

iServicePayroll.Pages.Home.Login我的登录页面