【问题标题】:Require Authenticated User (For entire app)需要经过身份验证的用户(对于整个应用程序)
【发布时间】:2021-06-12 23:59:16
【问题描述】:

如何将我的 Blazor WebAssembly 配置为要求对整个应用程序进行身份验证的用户,而不是使用 [Authorize] 属性标记每个页面或控制器?

我的应用应该只允许授权用户,并且推荐的配置在重定向到登录之前加载一点页面。

【问题讨论】:

  • 如何定义:public class AuthComponentBase: ComponentBase[Authorize] 并从中继承您的组件?

标签: c# authentication authorization blazor blazor-webassembly


【解决方案1】:

如果您的应用是独立的,一个简单的方法就是在 pages 文件夹中添加一个新的 _Imports.razor。 然后把这一行放进去。

@attribute [Authorize]

如果它是自托管的。 试试这个link 在浏览器中添加没有令牌的全局身份验证。

【讨论】:

    【解决方案2】:

    如何将我的 Blazor WebAssembly 配置为要求对整个应用程序进行身份验证的用户,而不是使用 [Authorize] 属性标记每个页面或控制器?

    您可以在主页加载时检查身份验证状态并决定下一步做什么。

    我的应用应该只允许授权用户,并且推荐的配置在重定向到登录之前加载一点页面。

    将下面的代码复制到Index.razor

    @inject NavigationManager Navigation
    @code{ 
    
        [CascadingParameter]
        private Task<AuthenticationState> authenticationStateTask { get; set; }
    
        protected async override Task OnInitializedAsync()
        {
            base.OnInitialized();
    
            var authState = await authenticationStateTask;
            var user = authState.User;
    
            if(!user.Identity.IsAuthenticated)
            {
                Navigation.NavigateTo($"authentication/login?returnUrl={Uri.EscapeDataString(Navigation.Uri)}");
            }
        }
    }
    

    有一些other solutions建议在不需要授权的页面上在_Imports.cs@attribute [Microsoft.AspNetCore.Authorization.AllowAnonymous]上添加@attribute [Microsoft.AspNetCore.Authorization.Authorize]。这从来没有像预期的那样对我有用,但很高兴知道它。

    【讨论】:

      【解决方案3】:

      尝试在您的布局页面中使用[Authorize]

      【讨论】:

        【解决方案4】:

        我的应用应该只允许授权用户,并且推荐的配置在重定向到登录之前加载一点页面。

        您应该在 App.razor 文件中执行此操作。它将在其他任何事情之前执行,并确保用户在呈现任何其他组件之前经过身份验证和授权。

        <CascadingAuthenticationState>
            <Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
                <Found Context="routeData">
                    <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
                        <NotAuthorized>
                            @if (context.User.Identity != null && !context.User.Identity.IsAuthenticated)
                            {
                                <RedirectToLogin />
                            }
                            else
                            {
                                <p>You are not authorized to access this resource.</p>
                            }
                        </NotAuthorized>
                        <Authorizing>
                            <Loader />
                        </Authorizing>
                    </AuthorizeRouteView>
                </Found>
                <NotFound>
                    <LayoutView Layout="@typeof(MainLayout)">
                        <div class="row justify-content-center">
                            <div class="col text-center">
                                <h2>Looks like you're lost, we didn't find anything at this location.</h2>
                                <p>Try navigating to the Dashboard or going back in the browser.</p>
                            </div>
                        </div>
                    </LayoutView>
                </NotFound>
            </Router>
        </CascadingAuthenticationState>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-07-15
          • 1970-01-01
          • 2016-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-15
          • 2016-01-17
          相关资源
          最近更新 更多