Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.NET 5 Blazor wasm单机版存在身份验证库注销问题_Blazor_.net 5_Blazor Webassembly_Blazor Client Side - Fatal编程技术网

.NET 5 Blazor wasm单机版存在身份验证库注销问题

.NET 5 Blazor wasm单机版存在身份验证库注销问题,blazor,.net-5,blazor-webassembly,blazor-client-side,Blazor,.net 5,Blazor Webassembly,Blazor Client Side,正如微软文档所描述的,我已经通过谷歌遵循了身份验证的安全设置 我可以登录,但在尝试注销时不断出现错误: 我的URL被重定向到: https://localhost:5001/authentication/logout-失败?消息=20%20注销%20不是%20从%20在%20页内%20启动的%20 我收到以下错误: 尝试将您注销时出错:“” 我的注销代码非常基本: 注销按钮和导航: <AuthorizeView> <Authorized>

正如微软文档所描述的,我已经通过谷歌遵循了身份验证的安全设置

我可以登录,但在尝试注销时不断出现错误:

我的URL被重定向到:

https://localhost:5001/authentication/logout-失败?消息=20%20注销%20不是%20从%20在%20页内%20启动的%20

我收到以下错误:

尝试将您注销时出错:“”

我的注销代码非常基本:

注销按钮和导航:

<AuthorizeView>
            <Authorized>
                <button class="nav-link" @onclick="(e) => SignOut(e)">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="white" width="24px" height="24px"><path d="M0 0h24v24H0z" fill="none" /><path d="M17 7l-1.41 1.41L18.17 11H8v2h10.17l-2.58 2.58L17 17l5-5zM4 5h8V3H4c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h8v-2H4V5z" /></svg>
                </button>
            </Authorized>
        </AuthorizeView>

private async Task SignOut(MouseEventArgs args)
    {
        await SignOutManager.SetSignOutState();
        NavManager.NavigateTo("/authentication/logout", true);
    }
如果我理解正确,这应该足以注销

我做错了什么

谢谢你抽出时间

编辑

我的应用程序设置配置为:

"Authentication": {
    "Google": {
      "Authority": "https://accounts.google.com/",
      "ClientId": "XXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com",
      "ClientSecret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
      "PostLogoutRedirectUri": "https://localhost:5001/authentication/logout-callback",
      "RedirectUri": "https://localhost:5001/authentication/login-callback",
      "ResponseType": "id_token",
    }
}
@inherits LayoutComponentBase

@using DoIt.ComponentLibrary.Modal;

<AuthorizeView>
    <Authorized>
        <Modal />
        <NavMenu />
        <main>
            @Body
        </main>
    </Authorized>
    <NotAuthorized>
        <RedirectToLogin></RedirectToLogin>
        @Body
    </NotAuthorized>
</AuthorizeView>
我的program.cs文件将其称为:

builder.Services.AddOidcAuthentication(options =>
            {
                builder.Configuration.Bind("Authentication:Google", options.ProviderOptions);
            });
我修改了我的代码:

NavManager.NavigateTo("/authentication/logout", true);

这并没有改变任何事情但你仍然必须这样使用它。

我的控制台日志正在打印以下信息消息:

信息: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2] 授权失败。这些要求没有得到满足: DenyAnonymousAuthorizationRequirement:需要经过身份验证的用户

编辑2

重要缺失信息

My MainLayout.razor is configured as:

@inherits LayoutComponentBase

@using DoIt.ComponentLibrary.Modal;

<AuthorizeView>
    <Authorized>
        <Modal />
        <NavMenu />
        <main>
            @Body
        </main>
    </Authorized>
    <NotAuthorized>
        <RedirectToLogin></RedirectToLogin>
        @Body
    </NotAuthorized>
</AuthorizeView>
My MainLayout.razor配置为:
@继承LayoutComponentBase
@使用DoIt.ComponentLibrary.Modal;
@身体
@身体

My MainLayout.razor配置为:

"Authentication": {
    "Google": {
      "Authority": "https://accounts.google.com/",
      "ClientId": "XXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com",
      "ClientSecret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
      "PostLogoutRedirectUri": "https://localhost:5001/authentication/logout-callback",
      "RedirectUri": "https://localhost:5001/authentication/login-callback",
      "ResponseType": "id_token",
    }
}
@inherits LayoutComponentBase

@using DoIt.ComponentLibrary.Modal;

<AuthorizeView>
    <Authorized>
        <Modal />
        <NavMenu />
        <main>
            @Body
        </main>
    </Authorized>
    <NotAuthorized>
        <RedirectToLogin></RedirectToLogin>
        @Body
    </NotAuthorized>
</AuthorizeView>
@继承LayoutComponentBase
@使用DoIt.ComponentLibrary.Modal;
@身体
@身体
在我的NavMenu组件下,我配置了如上所述的注销功能:

    <AuthorizeView>
            <Authorized>
                <button class="nav-link" @onclick="(e) => SignOut(e)">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="white" width="24px" height="24px"><path d="M0 0h24v24H0z" fill="none" /><path d="M17 7l-1.41 1.41L18.17 11H8v2h10.17l-2.58 2.58L17 17l5-5zM4 5h8V3H4c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h8v-2H4V5z" /></svg>
                </button>
            </Authorized>
        </AuthorizeView>

private async Task SignOut(MouseEventArgs args)
    {
        await SignOutManager.SetSignOutState();
        NavManager.NavigateTo("/authentication/logout", true);
    }

专用异步任务签出(MouseEventArgs args)
{
等待SignOutManager.SetSignOutState();
NavManager.NavigateTo(“/authentication/logout”,true);
}
这里的问题是:

  • 我按下退出按钮
  • 页面被导航到/授权/注销
  • 在这个过程中,应用程序注销->将被删除,因为我不再具有查看此组件的权限
  • 注销功能未完全完成,并且在从UI中删除调用组件时被中断
  • 出现上述错误
  • ->在我看来,这似乎是正在发生的事情。我找不到证实这一点的文档,因为与此流程相关的文档非常有限

    我通过将NavMenuBody组件从AuthorizeView组件中取出,解决了这个问题

    希望能有帮助

    编辑

    真正的解决方案可以在@enet

    @inherits LayoutComponentBase解释的描述中找到
    
    @inherits LayoutComponentBase
    
    @using DoIt.ComponentLibrary.Modal;
    
    <AuthorizeView>
        <Authorized>
            <Modal />
            <NavMenu />
            <main>
                @Body
            </main>
        </Authorized>
        <NotAuthorized>
            <RedirectToLogin></RedirectToLogin>
            @Body
        </NotAuthorized>
    </AuthorizeView>
    
    @使用DoIt.ComponentLibrary.Modal; @身体 @身体

    我在您的代码中看到的唯一问题是在非授权委托中使用@Body。它本来就不应该在那里。问题是,在您注销后,执行NotAuthorized委托后,重定向到登录组件,之后呈现系统会立即尝试呈现索引组件,因为它会看到@Body属性,但遗憾的是,您无权查看索引页面。。。我不知道是什么让你在那里使用它,但你当然应该把它拿走。至于导航菜单;这没有问题,它应该作为主布局呈现的一部分保留在那里

    我在代码中看到的唯一问题是在未授权的委托中使用@Body。它本来就不应该在那里。问题是,在您注销后,执行NotAuthorized委托后,重定向到登录组件,然后呈现系统立即尝试呈现索引组件,因为它看到了@Body属性,但遗憾的是,您无权查看索引页。。。我不知道是什么让你在那里使用它,但当然你应该删除它…至于导航菜单;这一点没有问题,它应该作为主布局渲染的一部分保留在那里。这似乎工作正常!美好的谢谢你的帮助!如果你把它贴在一个新的答案里,我会把它标记为这个主题的解决方案。
    @inherits LayoutComponentBase
    
    @using DoIt.ComponentLibrary.Modal;
    
    <AuthorizeView>
        <Authorized>
            <Modal />
            <NavMenu />
            <main>
                @Body
            </main>
        </Authorized>
        <NotAuthorized>
            <RedirectToLogin></RedirectToLogin>
            @Body
        </NotAuthorized>
    </AuthorizeView>