AuthorizeView & Authorized enbracing into LayoutComponent

Hi all,

How to use properly AuthorizeView into a blazor component that inherits LayoutComponentBase?

The code snippet below does not work to trigger my Identity server login

<AuthorizeView Roles="HR Admin">
    <Authorized>
      <RadzenLayout class="rz-shadow-3">
          <RadzenHeader>
              <div class="d-flex align-items-center">
                  <RadzenSidebarToggle Click="@(() => sidebarExpanded = !sidebarExpanded)" />
                  <RadzenLabel Text="Recursos Humanos" />
              </div>
          </RadzenHeader>
          <RadzenSidebar @bind-Expanded="@sidebarExpanded">
              <RadzenPanelMenu>
                  <RadzenPanelMenuItem Text="Fundos de Compensação" Icon="home" Path="@navigationManager?.Uri" />
              </RadzenPanelMenu>
          </RadzenSidebar>
          <RadzenBody>
              <div class="p-2">
                  @Body
              </div>
          </RadzenBody>
          <RadzenFooter>
               <Footer />
          </RadzenFooter>
      </RadzenLayout>

Hi @Alexandre_Seneda,

You can try removing RadzenLayout to see if it makes a difference.I don't think RadzenLAyout can in any way prevent AuthorizeView from working.

Hi @korchev ,

Thanks for the quick feedback,

I just put the Radzen body outside of AuthorizeView as in the image below and so, it works.

Any thoughts on why?

Best regards

Unfortunately I don't know. All I know is that RadzenLayout does is to render its children and does interfere with AuthorizeView. Also I am not sure that AuthorizeView ever redirects to the login. I couldn't find such code in its implementation but I could be wrong. Again try removing all Radzen components and leave just @Body to see if it works.

Using only :

@Body

Does not work

I suspected so. You should probably find another way to redirect to the login. Here is how Radzen applications do it:

App.razor

<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly">
    <Found Context="routeData">
        <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
            <NotAuthorized>
                <RedirectToLogin IsAuthenticated="@context.User.Identity.IsAuthenticated" />
            </NotAuthorized>
        </AuthorizeRouteView>
    </Found>
    <NotFound>
        <PageTitle>Not found</PageTitle>
        <LayoutView Layout="@typeof(MainLayout)">
             Not found
        </LayoutView>
    </NotFound>
</Router>
</CascadingAuthenticationState>

RedirectToLogin.razor

@inject NavigationManager NavigationManager

@code {
    [Parameter]
    public bool IsAuthenticated { get; set; }

    protected override void OnInitialized()
    {
        if (!IsAuthenticated)
        {
            var redirectUrl = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
            if (!string.IsNullOrEmpty(redirectUrl))
            {
                NavigationManager.NavigateTo($"Login?redirectUrl={Uri.EscapeDataString(redirectUrl)}", true);
            }
            else
            {
                NavigationManager.NavigateTo("Login", true);
            }
        }
        else
        {
            NavigationManager.NavigateTo("Unauthorized");
        }
    }
}

Thanks, @korchev for all support.