Redirect to login show menu before showing login

Hello,

I have deployed the blazor webpage inside docker and with traefik as proxy.

The problem is that when I go to my web page, first of all shows de menu and then redirect to the login page:

You can check this behaviour click the following url:
https://dev.2certs.com/

How can I solve it?

Thank you very much.

This happens because of the way security works. It checks if the user is logged in and then redirects to the login page. Sometimes this doesn't happen quickly enough and parts of the layout could render. We aren't aware of a solution for that as Blazor starts rendering as soon as there is await code in OnInitializedAsync.

For what is worth I couldn't see it on the provided URL. Here is a recording of my experience:

login

When I browse to the provided link I can clearly see the whole menu and then it redirect to the login.

See if this can help you in any way

Thank you! I will try this aproach!

All Radzen Blazor applications have a RedirectToLogin component:

@inject NavigationManager UriHelper

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

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

And It is used in App.razor

<CascadingAuthenticationState>
<Router AppAssembly="typeof(Startup).Assembly">
    <Found Context="routeData">
            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
                <NotAuthorized>
                    <RedirectToLogin IsAuthenticated="@context.User.Identity.IsAuthenticated" />
                </NotAuthorized>
            </AuthorizeRouteView>
    </Found>
    <NotFound>
        <LayoutView Layout="typeof(MainLayout)">
            <h1>Page not found</h1>
            <p>Sorry, but there's nothing here!</p>
        </LayoutView>
    </NotFound>
</Router>
</CascadingAuthenticationState>

The thread category is set as Radzen.Blazor components so I am not sure if @zarroc is using Radzen or not.

Yes I am using Blazor

Sorry I mean, I am using Radzen (with blazor obviously)

In that case your application already contains the solution suggested in the Stackoverflow question. Sadly we are not aware of a workaround. It seems Blazor just starts rendering the output before finishing the security check - it doesn't block the thread.

As a workaround, I added AuthorizeView to MainLayout.razor and achieved the desired result; the user no longer sees anything if he is not logged in.

@inherits LayoutComponentBase
<RadzenDialog />
<RadzenNotification />
<RadzenTooltip />
<RadzenContextMenu />

<AuthorizeView>

	<NotAuthorized>
		<RedirectToLogin IsAuthenticated="@context.User.Identity.IsAuthenticated" />
	</NotAuthorized>

	<Authorized>
		<RadzenLayout>
		.....
		.....
		</RadzenLayout>
	</Authorized>
</AuthorizeView>

Hello Atanas,
Using UriHelper.NavigateTo within OnInitialized() caused exceptions for me sometimes, I think it is better to change the event to OnAfterRender if I am not mistaken.

OnAfterRender is too late IMHO - the page will render completely and there will be a more noticeable flicker. This is a known issue that Microsoft seems to ignore for some reason.

Having AuthorizeView in the Layout sounds as a viable workaround. The only side effect is that you can't have anonymous pages anymore (that use this layout).

I agree; OnAfterRender is too late for sure. We have to wait until Microsoft fixes this issue.

For the AuthorizeView in MainLayout, yes, you are right, It cannot be used for anonymous pages. I simply duplicated the original layout as AnonymousLayout.razor and used it when needed for anonymous pages.