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.
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.
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.